Putting It All Together
Use the examples we’ve seen already, we can create an application that shows some more applications of the LINQ operators. The following VB code defines the Customer class:
Class Customer
Private _Name As String
Private _Age As Integer
Private _Gender As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property Age() As Integer
Get
Return _Age
End Get
Set(ByVal value As Integer)
_Age = value
End Set
End Property
Public Property Gender() As String
Get
Return _Gender
End Get
Set(ByVal value As String)
_Gender = value
End Set
End Property
End Class
We can now create a list of customers. (It would have been marginally quicker to do this in C#, because at the time of writing, VB.NET does not support collection initialization. However, both VB.NET and C# support the same kind of member initialization.) This is the collection we will be querying against:
Dim Customers = New List(Of Customer)
Customers.Add(New Customer With {.Name = "Sarah Jenkins", _
.Age = 14, .Gender = "Female"})
Customers.Add(New Customer With {.Name = "John Fowler", _
.Age = 24, .Gender = "Male"})
Customers.Add(New Customer With {.Name = "Charles Dodgson", _
.Age = 24, .Gender = "Male"})
Customers.Add(New Customer With {.Name = "Alice Liddell", _
.Age = 24, .Gender = "Female"})
Customers.Add(New Customer With {.Name = "Tricia McMillan", _
.Age = 34, .Gender = "Female"})
Customers.Add(New Customer With {.Name = "Patrick Sweeney", _
.Age = 24, .Gender = "Male"})
The following code will select all male customers and order the collection by name, in reverse alphabetical order:
Dim MaleCustomers = From EachCustomer In Customers _
Where EachCustomer.Gender = "Male" _
Order By EachCustomer.Name Descending _
Select EachCustomer
Console.WriteLine("Male Customers:")
For Each Man In MaleCustomers
Console.WriteLine(Man.Name)
Next
This produces the following output:
Male Customers:
Patrick Sweeney
John Fowler
Charles Dodgson
The following code uses the GroupBy operator to group customers on the basis of their gender. It will display the list of all female customers first, and then the list of all male customers.
Dim CustomersByGender = From EachCustomer In Customers _
Group By EachCustomer.Gender Into Group _
Select New With _
{.Count = Group.Count(), .Members = Group}
For Each GenderGroup In CustomersByGender
For Each EachCustomer In GenderGroup.Members
Console.WriteLine(EachCustomer.Name & vbTab & EachCustomer.Gender)
Next
Next
This produces the following output:
Sarah Jenkins Female
Alice Liddell Female
Tricia McMillan Female
John Fowler Male
Charles Dodgson Male
Patrick Sweeney Male
To count the number of records that have age > 20 years, we can use the following code:
Dim ValidCustomersCount = (From EachCustomer In Customers _
Where EachCustomer.Age >= 18).Count()
Console.WriteLine("Number of Adult Customers:")
Console.WriteLine(ValidCustomersCount)
This produces the following output:
Number of Adult Customers:
5
|
No comments:
Post a Comment