JayBeeOH

LINQ To Objects Demo

May 6th, 2016
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.57 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   LINQ to Objects Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   06 MAY 2016
  16. '
  17. ' Description:    Demonstrate filtering, sorting and other LINQ extensions when working
  18. '                 with LINQ to Objects.
  19.  
  20. '
  21. '                 Documentation is at:
  22. '                   App's screen image is at: http://imgur.com/C5SAryx
  23. '                   App's Visual Basic .NET code is at http://pastebin.com/uCVVjW12
  24. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  25. '-------------------------------------------------------------------------------------------
  26.  
  27. Option Strict On
  28. Option Infer On
  29.  
  30. Imports System.Text             ' For StringBuilder class
  31.  
  32. Public Class LinqForm
  33.  
  34.     Private Sub LINQIntButton_Click(sender As Object, e As EventArgs) _
  35.         Handles LINQIntButton.Click
  36.  
  37.         ' Declare an integer array with initalization list.
  38.         Dim intArray() As Integer = {22, 53, 71, 17, -15, 63}
  39.  
  40.         ' Process as a typical array for output.
  41.         Dim output As New StringBuilder("Original values in intArray:" & vbNewLine)
  42.         For Idx As Integer = 0 To intArray.GetUpperBound(0)
  43.             output.Append(intArray(Idx).ToString() & " ")
  44.         Next
  45.  
  46.  
  47.         ' Process as a LINQ query for odd numbers only.
  48.         Dim oddNumbers = From num In intArray
  49.                          Where num Mod 2 <> 0
  50.                          Select num
  51.  
  52.         output.AppendLine(String.Format("{0}{0}{1}", vbNewLine, "Odd numbers in intArray:"))
  53.         For Each element In oddNumbers
  54.             output.Append(element.ToString() & " ")
  55.         Next
  56.  
  57.  
  58.         ' Process as a LINQ query to get the first four elements.
  59.         Dim firstFour = From num In intArray
  60.                         Take 4
  61.                         Select num
  62.  
  63.         output.AppendLine(String.Format("{0}{0}{1}", vbNewLine, "First four numbers in intArray:"))
  64.         For Each element In firstFour
  65.             output.Append(element.ToString() & " ")
  66.         Next
  67.  
  68.  
  69.         ' Process as a LINQ extensions for sum, average, minimum & maximum.
  70.         output.AppendLine(String.Format("{0}{0}{1}", vbNewLine, "Summary of intArray:"))
  71.         output.AppendLine(String.Format("{0,10}{1,6:N0}", "Sum:", intArray.Sum))
  72.         output.AppendLine(String.Format("{0,10}{1,6:N1}", "Average:", intArray.Average))
  73.         output.AppendLine(String.Format("{0,10}{1,6:N0}", "Minimum:", intArray.Min))
  74.         output.AppendLine(String.Format("{0,10}{1,6:N0}", "Maximum:", intArray.Max))
  75.  
  76.         ' Assign stringbuilder output to ResultsTextBox.
  77.         ResultsTextBox.Text = output.ToString
  78.  
  79.     End Sub
  80.  
  81.     Private Sub LINQStrButton_Click(sender As Object, e As EventArgs) _
  82.         Handles LINQStrButton.Click
  83.  
  84.         ' Declare an string array with initalization list.
  85.         Dim strArray() As String = {"Joe Bolen",
  86.                                     "Sally Peterson",
  87.                                     "Frank Walman",
  88.                                     "Steve Peterson",
  89.                                     "Dan Selby",
  90.                                     "Cathy Cunningham",
  91.                                     "Tim Hamilton"}
  92.  
  93.         ' Process as a typical array for output.
  94.         Dim output As New StringBuilder("Original values in strArray:" & vbNewLine)
  95.         For Idx As Integer = 0 To strArray.GetUpperBound(0)
  96.             output.AppendLine(vbTab & strArray(Idx))
  97.         Next
  98.  
  99.  
  100.         ' Process as a LINQ query skipping the first 3 elements.
  101.         Dim skipFirst3 = From name In strArray
  102.                          Skip 3
  103.                          Select name
  104.  
  105.         output.AppendLine(String.Format("{0}{1}", vbNewLine, "Skipping the first three elements in strArray:"))
  106.         For Each element In skipFirst3
  107.             output.AppendLine(vbTab & element)
  108.         Next
  109.  
  110.         ' Process as a LINQ query to sort by last name then first name.
  111.         Dim sortedNames = From name In strArray
  112.                           Let fullname = name.Split(Convert.ToChar(" "))
  113.                           Order By fullname(1), fullname(0)
  114.                           Select fullname(1) & ", " & fullname(0)
  115.  
  116.         output.AppendLine(String.Format("{0}{1}", vbNewLine, "Sorting names in strArray:"))
  117.         For Each element In sortedNames
  118.             output.AppendLine(vbTab & element)
  119.         Next
  120.  
  121.         ' Assign stringbuilder output to ResultsTextBox.
  122.         ResultsTextBox.Text = output.ToString
  123.     End Sub
  124.  
  125.     Private Sub LINQListButton_Click(sender As Object, e As EventArgs) _
  126.         Handles LINQListButton.Click
  127.  
  128.         ' Get IEnumerable(Of Student)
  129.         Dim students = Student.GetStudents()
  130.  
  131.         ' Process raw student list for output.
  132.         Dim output As New StringBuilder("Unsorted Student Listing:" & vbNewLine)
  133.         For Each s In students
  134.             output.AppendLine(vbTab & s.ToString())
  135.         Next
  136.  
  137.         Dim sortedStudents = From s In students
  138.                              Order By s.LastName, s.FirstName
  139.                              Select s
  140.  
  141.         ' Process student list for output.
  142.         output.AppendLine(String.Format("{0}{1}", vbNewLine, "Sorted Student Listing:"))
  143.         For Each s In sortedStudents
  144.             output.AppendLine(vbTab & s.ToString())
  145.         Next
  146.  
  147.         ' Process Presidents List.
  148.         Dim presidentsList = From s In students
  149.                              Where s.GPA = 4.0
  150.                              Order By s.LastName, s.FirstName
  151.                              Select s
  152.  
  153.         ' Process student list for output.
  154.         output.AppendLine(String.Format("{0}{1}", vbNewLine, "President's List (Sorted & Filtered):"))
  155.         For Each s In presidentsList
  156.             output.AppendLine(vbTab & s.ToString())
  157.         Next
  158.  
  159.         ' Process Deans List.
  160.         Dim deansList = From s In students
  161.                         Where s.GPA < 3.99 AndAlso s.GPA > 3.25
  162.                         Order By s.LastName, s.FirstName
  163.                         Select s
  164.  
  165.  
  166.         ' Process student list for output.
  167.         output.AppendLine(String.Format("{0}{1}", vbNewLine, "Dean's List (Sorted & Filtered):"))
  168.         For Each s In deansList
  169.             output.AppendLine(vbTab & s.ToString())
  170.         Next
  171.  
  172.         ' Assign stringbuilder output to ResultsTextBox.
  173.         ResultsTextBox.Text = output.ToString
  174.     End Sub
  175.  
  176. End Class
  177.  
  178. Public Class Student
  179.  
  180. #Region "     Class Properties"
  181.  
  182.     Private firstNameValue As String
  183.     Public Property FirstName() As String
  184.         Get
  185.             Return firstNameValue
  186.         End Get
  187.         Set(ByVal value As String)
  188.             If String.IsNullOrWhiteSpace(value) Then
  189.                 Throw New ArgumentNullException("First Name must have a value.")
  190.             Else
  191.                 firstNameValue = value
  192.             End If
  193.         End Set
  194.     End Property
  195.  
  196.     Private lastNameValue As String
  197.     Public Property LastName() As String
  198.         Get
  199.             Return lastNameValue
  200.         End Get
  201.         Set(ByVal value As String)
  202.             If String.IsNullOrWhiteSpace(value) Then
  203.                 Throw New ArgumentNullException("Last Name must have a value.")
  204.             Else
  205.                 lastNameValue = value
  206.             End If
  207.         End Set
  208.     End Property
  209.  
  210.     Private gpaValue As Double
  211.     Public Property GPA() As Double
  212.         Get
  213.             Return gpaValue
  214.         End Get
  215.         Set(ByVal value As Double)
  216.             If value < 0 OrElse value > 4 Then
  217.                 Throw New ArgumentOutOfRangeException("GPA must be between 0 and 4.")
  218.             Else
  219.                 gpaValue = value
  220.             End If
  221.         End Set
  222.     End Property
  223.  
  224. #End Region
  225.  
  226. #Region "     Class Constructors"
  227.  
  228.     ' Default Constructor
  229.     Public Sub New()
  230.  
  231.     End Sub
  232.  
  233.     ' Overloaded Constructor
  234.     Public Sub New(ByVal fName As String, ByVal lName As String, ByVal currentGPA As Double)
  235.         FirstName = fName
  236.         LastName = lName
  237.         GPA = currentGPA
  238.     End Sub
  239.  
  240. #End Region
  241.  
  242. #Region "     Public Methods and Functions"
  243.  
  244.     ' Function GetStudents returns a list of Student objects.
  245.     ' Note: If you wish to add to the collection, use a List(Of Student) instead.
  246.     Public Shared Function GetStudents() As IEnumerable(Of Student)
  247.         Return New List(Of Student) From {
  248.             New Student("Joe", "Bolen", 3.57),
  249.             New Student("Sally", "Peterson", 3.2),
  250.             New Student("Fank", "Walman", 3.3),
  251.             New Student("Steve", "Peterson", 2.95),
  252.             New Student("Dan", "Selby", 4.0),
  253.             New Student("Cathy", "Cunningham", 3.28),
  254.             New Student("Tim", "Hamilton", 2.45)
  255.         }
  256.     End Function
  257.  
  258.     Public Overrides Function ToString() As String
  259.         Return String.Format("{0}, {1} {2} {3:N2}", LastName, FirstName, "- GPA:", GPA)
  260.     End Function
  261.  
  262. #End Region
  263.  
  264. End Class
Advertisement
Add Comment
Please, Sign In to add comment