Web developer can use LINQ group clause to group his/her results based on a key that he/she specifies. For example Web developer could specify that the results should be grouped by the City so that all clients from New York or San Francisco are in individual groups. In this case client.City is the key.
Dim qryClientsByCity = From client In clients
Group By ClientCity = client.City
Into ClientGroup = Group
Select ClientGroup
For Each ClientGroup In qryClientsByCity
For Each client As Client In ClientGroup
Console.WriteLine(“{0}”, client.FirstName)
Next
Next
When Web developer ends a query with a group clause, his/her results take the form of list of lists. Each element in the list is an object that has a Key member and a list of elements that are grouped under that key. When Web developer iterates over a query that produces a sequence of groups, he/she has to use an outer loop to iterate over each group and the inner loop to iterate over each group’s members.
If Web developer has to refer to the results of a group operation, he/she can use the into keyword to create an identifier that can be queried further. The following query returns only those groups that contain more than two clients:
‘clientQuery is an IEnumerable<IGrouping<string, Client>>
var clientQuery =
from client in clients
group client by client.City into clientGroup
where clientGroup.Count() > 2
orderby clientGroup.Key
select clientGroup