When you create grouping of values, the resulting list is stored in a variable of type IGrouping. The IGrouping interface is defined in the System.Linq namespace of the System.Core.dll assembly. The IGrouping interface is derived from the IEnumerable interface. This means that it gets most of its behaviors from that interface. This also means that using the IGrouping interface gives you access to the members of the Enumerable class. The IGrouping interface is a generic class declared as follows: Public Interface IGrouping(Of Out TKey, Out TElement) _ Inherits IEnumerable(Of TElement), IEnumerable In our introduction to grouping, we saw that its operation identifies the categories of items from the From variable. Each category is referred to as a key and each category can be recognized as a TKey object of the IGrouping list. This allows you to access each category. In fact, you can access a category and perform an operation on it. Although the IGrouping interface inherits most of its functionality from the IEnumerable interface and implemented through the Enumerable class, it is equipped with only one property, named Key. To get the value of an IGrouping category, you can retrieve it from the Key property. After creating grouping name, you can perform any operation on it inside the LINQ statement. The variable is of type IGrouping. This means that you can access its Key property or you can access one of the methods that the interface gets from IEnumerable, and then use it as you see fit. Before ending the LINQ statement, you must create either a Group...By expression or a Select statement that uses the Into variable. Here is an example: Imports System.Linq Imports System.Collections.Generic Public Enum Genders Female Male Unknown End Enum Public Class Employee Public EmployeeNumber As Integer Public FirstName As String Public LastName As String Public Gender As Genders Public HourlySalary As Double Public Sub New(Optional ByVal Number As Integer = 0, Optional ByVal FName As String = "John", Optional ByVal LName As String = "Doe", Optional ByVal Gdr As Genders = Genders.Unknown, Optional ByVal salary As Double = 0D) EmployeeNumber = Number FirstName = FName LastName = LName Gender = Gdr HourlySalary = salary End Sub End Class Module Exercise Public Function Main() As Integer Dim Employees() As Employee = { New Employee(971974, "Patricia", "Katts", Genders.Female, 24.68), New Employee(408415, "Leslie", "Simms", Genders.Unknown, 15.72), New Employee(208411, "Raymond", "Kouma", Genders.Male, 20.15), New Employee(279374, "H�l�ne", "Mukoko", Genders.Female, 15.55), New Employee(607575, "Hermine", "Kensley", Genders.Female, 12.55), New Employee(707912, "Bertrand", "Yamaguchi", Genders.Male, 24.68), New Employee(475022, "Leslie", "Simms", Genders.Unknown, 15.72), New Employee(448660, "Helios", "Panko", Genders.Unknown, 12.69), New Employee(294800, "Peter", "Mukoko", Genders.Male, 18.85), New Employee(971394, "Gertrude", "Monay", Genders.Female, 20.55), New Employee(680404, "Lydia", "Kensley", Genders.Female, 22.58) } Dim Contractors = From StaffMembers In Employees Group StaffMembers By StaffMembers.Gender Into People = ToArray() Where People.Contains(Employees(0)) Select People Console.WriteLine("+========+============+===========+=========+========+") Console.WriteLine("| Empl # | First Name | Last Name | Gender | Salary |") Console.WriteLine("+========+============+===========+=========+========+") For Each Staff In Contractors For Each Employee In Staff Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,-7} | {4,6} |", Employee.EmployeeNumber, Employee.FirstName, Employee.LastName, Employee.Gender, Employee.HourlySalary) Console.WriteLine("+--------+------------+-----------+---------+--------+") Next Next Console.WriteLine() Return 0 End Function End Module This statement, particularly the Enumerable.Contains(Employees(0)) produces only the category (group) identified as the first index (0) of the values in the main list:
Notice that all records in the final result have a common category, which in this case is the F gender of each student. For this reason, you can omit that column when presenting the values to the user. Here is an example (the column for the rating was removed from the list view): Imports System.Linq Imports System.Collections.Generic Public Enum Genders Female Male Unknown End Enum Public Class Employee Public EmployeeNumber As Integer Public FirstName As String Public LastName As String Public Gender As Genders Public HourlySalary As Double Public Sub New(Optional ByVal Number As Integer = 0, Optional ByVal FName As String = "John", Optional ByVal LName As String = "Doe", Optional ByVal Gdr As Genders = Genders.Unknown, Optional ByVal salary As Double = 0D) EmployeeNumber = Number FirstName = FName LastName = LName Gender = Gdr HourlySalary = salary End Sub End Class Module Exercise Public Function Main() As Integer Dim Employees() As Employee = { New Employee(971974, "Patricia", "Katts", Genders.Female, 24.68), New Employee(408415, "Leslie", "Simms", Genders.Unknown, 15.72), New Employee(208411, "Raymond", "Kouma", Genders.Male, 20.15), New Employee(279374, "H�l�ne", "Mukoko", Genders.Female, 15.55), New Employee(607575, "Hermine", "Kensley", Genders.Female, 12.55), New Employee(707912, "Bertrand", "Yamaguchi", Genders.Male, 24.68), New Employee(475022, "Leslie", "Simms", Genders.Unknown, 15.72), New Employee(448660, "Helios", "Panko", Genders.Unknown, 12.69), New Employee(294800, "Peter", "Mukoko", Genders.Male, 18.85), New Employee(971394, "Gertrude", "Monay", Genders.Female, 20.55), New Employee(680404, "Lydia", "Kensley", Genders.Female, 22.58) } Dim Contractors = From StaffMembers In Employees Group StaffMembers By StaffMembers.Gender Into People = ToArray() Where People.Contains(Employees(0)) Select People Console.WriteLine("+========+============+===========+========+") Console.WriteLine("| Empl # | First Name | Last Name | Salary |") Console.WriteLine("+========+============+===========+========+") For Each Staff In Contractors For Each Employee In Staff Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,6} |", Employee.EmployeeNumber, Employee.FirstName, Employee.LastName, Employee.HourlySalary) Console.WriteLine("+--------+------------+-----------+--------+") Next Next Console.WriteLine() Return 0 End Function End Module This would produce:
In the same, to get the category stored in the second index of the grouping, you would use Enumerable.Contains(Employees(1)). Of course this means that you can use grouping and the Into operator to get a list of items of only one particular category. Although the GroupVariable can be Selected or Grouped...By, it cannot be used outside the LINQ statement. It is only available in the local LINQ expression. To restrict the list of records in the result, you can add a Where condition. Here is an example: Imports System.Linq Imports System.Collections.Generic Public Enum Genders Female Male Unknown End Enum Public Class Employee Public EmployeeNumber As Integer Public FirstName As String Public LastName As String Public Gender As Genders Public HourlySalary As Double Public Sub New(Optional ByVal Number As Integer = 0, Optional ByVal FName As String = "John", Optional ByVal LName As String = "Doe", Optional ByVal Gdr As Genders = Genders.Unknown, Optional ByVal salary As Double = 0D) EmployeeNumber = Number FirstName = FName LastName = LName Gender = Gdr HourlySalary = salary End Sub End Class Module Exercise Public Function Main() As Integer Dim Employees() As Employee = { New Employee(971974, "Patricia", "Katts", Genders.Female, 24.68), New Employee(408415, "Leslie", "Simms", Genders.Unknown, 15.72), New Employee(208411, "Raymond", "Kouma", Genders.Male, 20.15), New Employee(279374, "H�l�ne", "Mukoko", Genders.Female, 15.55), New Employee(607575, "Hermine", "Kensley", Genders.Female, 12.55), New Employee(707912, "Bertrand", "Yamaguchi", Genders.Male, 24.68), New Employee(475022, "Leslie", "Simms", Genders.Unknown, 15.72), New Employee(448660, "Helios", "Panko", Genders.Unknown, 12.69), New Employee(294800, "Peter", "Mukoko", Genders.Male, 18.85), New Employee(971394, "Gertrude", "Monay", Genders.Female, 20.55), New Employee(680404, "Lydia", "Kensley", Genders.Female, 22.58) } Dim Contractors = From StaffMembers In Employees Where StaffMembers.FirstName.StartsWith("H") Group StaffMembers By StaffMembers.Gender Into People = ToArray() Select People Console.WriteLine("+========+============+===========+========+") Console.WriteLine("| Empl # | First Name | Last Name | Salary |") Console.WriteLine("+========+============+===========+========+") For Each Staff In Contractors For Each Employee In Staff Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,6} |", Employee.EmployeeNumber, Employee.FirstName, Employee.LastName, Employee.HourlySalary) Console.WriteLine("+--------+------------+-----------+--------+") Next Next Console.WriteLine() Return 0 End Function End Module This would produce:
|
|
|||
|