Grouping Using the group by Clause

Often data can be grouped into buckets based on certain criteria. For example, products may be grouped by category, and people may be grouped by age range (for example, based on modulo 10 division of the age). The group by clause enables you to group objects by a key, resulting in a sequence of IGrouping<TKey, TValue> objects.


The grouping query operator is best looked at as a partitioning mechanism where items from the source sequence are grouped by matching key values. This key gets computed for all elements in the source sequence to realize this grouping. For example, we could partition a sequence of integer numbers by their remainder after division by, say, three. To put it in terms of grouping ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.