Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: LINQ to Objects Demo
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 06 MAY 2016
- '
- ' Description: Demonstrate filtering, sorting and other LINQ extensions when working
- ' with LINQ to Objects.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/C5SAryx
- ' App's Visual Basic .NET code is at http://pastebin.com/uCVVjW12
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Option Infer On
- Imports System.Text ' For StringBuilder class
- Public Class LinqForm
- Private Sub LINQIntButton_Click(sender As Object, e As EventArgs) _
- Handles LINQIntButton.Click
- ' Declare an integer array with initalization list.
- Dim intArray() As Integer = {22, 53, 71, 17, -15, 63}
- ' Process as a typical array for output.
- Dim output As New StringBuilder("Original values in intArray:" & vbNewLine)
- For Idx As Integer = 0 To intArray.GetUpperBound(0)
- output.Append(intArray(Idx).ToString() & " ")
- Next
- ' Process as a LINQ query for odd numbers only.
- Dim oddNumbers = From num In intArray
- Where num Mod 2 <> 0
- Select num
- output.AppendLine(String.Format("{0}{0}{1}", vbNewLine, "Odd numbers in intArray:"))
- For Each element In oddNumbers
- output.Append(element.ToString() & " ")
- Next
- ' Process as a LINQ query to get the first four elements.
- Dim firstFour = From num In intArray
- Take 4
- Select num
- output.AppendLine(String.Format("{0}{0}{1}", vbNewLine, "First four numbers in intArray:"))
- For Each element In firstFour
- output.Append(element.ToString() & " ")
- Next
- ' Process as a LINQ extensions for sum, average, minimum & maximum.
- output.AppendLine(String.Format("{0}{0}{1}", vbNewLine, "Summary of intArray:"))
- output.AppendLine(String.Format("{0,10}{1,6:N0}", "Sum:", intArray.Sum))
- output.AppendLine(String.Format("{0,10}{1,6:N1}", "Average:", intArray.Average))
- output.AppendLine(String.Format("{0,10}{1,6:N0}", "Minimum:", intArray.Min))
- output.AppendLine(String.Format("{0,10}{1,6:N0}", "Maximum:", intArray.Max))
- ' Assign stringbuilder output to ResultsTextBox.
- ResultsTextBox.Text = output.ToString
- End Sub
- Private Sub LINQStrButton_Click(sender As Object, e As EventArgs) _
- Handles LINQStrButton.Click
- ' Declare an string array with initalization list.
- Dim strArray() As String = {"Joe Bolen",
- "Sally Peterson",
- "Frank Walman",
- "Steve Peterson",
- "Dan Selby",
- "Cathy Cunningham",
- "Tim Hamilton"}
- ' Process as a typical array for output.
- Dim output As New StringBuilder("Original values in strArray:" & vbNewLine)
- For Idx As Integer = 0 To strArray.GetUpperBound(0)
- output.AppendLine(vbTab & strArray(Idx))
- Next
- ' Process as a LINQ query skipping the first 3 elements.
- Dim skipFirst3 = From name In strArray
- Skip 3
- Select name
- output.AppendLine(String.Format("{0}{1}", vbNewLine, "Skipping the first three elements in strArray:"))
- For Each element In skipFirst3
- output.AppendLine(vbTab & element)
- Next
- ' Process as a LINQ query to sort by last name then first name.
- Dim sortedNames = From name In strArray
- Let fullname = name.Split(Convert.ToChar(" "))
- Order By fullname(1), fullname(0)
- Select fullname(1) & ", " & fullname(0)
- output.AppendLine(String.Format("{0}{1}", vbNewLine, "Sorting names in strArray:"))
- For Each element In sortedNames
- output.AppendLine(vbTab & element)
- Next
- ' Assign stringbuilder output to ResultsTextBox.
- ResultsTextBox.Text = output.ToString
- End Sub
- Private Sub LINQListButton_Click(sender As Object, e As EventArgs) _
- Handles LINQListButton.Click
- ' Get IEnumerable(Of Student)
- Dim students = Student.GetStudents()
- ' Process raw student list for output.
- Dim output As New StringBuilder("Unsorted Student Listing:" & vbNewLine)
- For Each s In students
- output.AppendLine(vbTab & s.ToString())
- Next
- Dim sortedStudents = From s In students
- Order By s.LastName, s.FirstName
- Select s
- ' Process student list for output.
- output.AppendLine(String.Format("{0}{1}", vbNewLine, "Sorted Student Listing:"))
- For Each s In sortedStudents
- output.AppendLine(vbTab & s.ToString())
- Next
- ' Process Presidents List.
- Dim presidentsList = From s In students
- Where s.GPA = 4.0
- Order By s.LastName, s.FirstName
- Select s
- ' Process student list for output.
- output.AppendLine(String.Format("{0}{1}", vbNewLine, "President's List (Sorted & Filtered):"))
- For Each s In presidentsList
- output.AppendLine(vbTab & s.ToString())
- Next
- ' Process Deans List.
- Dim deansList = From s In students
- Where s.GPA < 3.99 AndAlso s.GPA > 3.25
- Order By s.LastName, s.FirstName
- Select s
- ' Process student list for output.
- output.AppendLine(String.Format("{0}{1}", vbNewLine, "Dean's List (Sorted & Filtered):"))
- For Each s In deansList
- output.AppendLine(vbTab & s.ToString())
- Next
- ' Assign stringbuilder output to ResultsTextBox.
- ResultsTextBox.Text = output.ToString
- End Sub
- End Class
- Public Class Student
- #Region " Class Properties"
- Private firstNameValue As String
- Public Property FirstName() As String
- Get
- Return firstNameValue
- End Get
- Set(ByVal value As String)
- If String.IsNullOrWhiteSpace(value) Then
- Throw New ArgumentNullException("First Name must have a value.")
- Else
- firstNameValue = value
- End If
- End Set
- End Property
- Private lastNameValue As String
- Public Property LastName() As String
- Get
- Return lastNameValue
- End Get
- Set(ByVal value As String)
- If String.IsNullOrWhiteSpace(value) Then
- Throw New ArgumentNullException("Last Name must have a value.")
- Else
- lastNameValue = value
- End If
- End Set
- End Property
- Private gpaValue As Double
- Public Property GPA() As Double
- Get
- Return gpaValue
- End Get
- Set(ByVal value As Double)
- If value < 0 OrElse value > 4 Then
- Throw New ArgumentOutOfRangeException("GPA must be between 0 and 4.")
- Else
- gpaValue = value
- End If
- End Set
- End Property
- #End Region
- #Region " Class Constructors"
- ' Default Constructor
- Public Sub New()
- End Sub
- ' Overloaded Constructor
- Public Sub New(ByVal fName As String, ByVal lName As String, ByVal currentGPA As Double)
- FirstName = fName
- LastName = lName
- GPA = currentGPA
- End Sub
- #End Region
- #Region " Public Methods and Functions"
- ' Function GetStudents returns a list of Student objects.
- ' Note: If you wish to add to the collection, use a List(Of Student) instead.
- Public Shared Function GetStudents() As IEnumerable(Of Student)
- Return New List(Of Student) From {
- New Student("Joe", "Bolen", 3.57),
- New Student("Sally", "Peterson", 3.2),
- New Student("Fank", "Walman", 3.3),
- New Student("Steve", "Peterson", 2.95),
- New Student("Dan", "Selby", 4.0),
- New Student("Cathy", "Cunningham", 3.28),
- New Student("Tim", "Hamilton", 2.45)
- }
- End Function
- Public Overrides Function ToString() As String
- Return String.Format("{0}, {1} {2} {3:N2}", LastName, FirstName, "- GPA:", GPA)
- End Function
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment