📎 Webclip
LINQ GroupBy in Depth
The page walks through LINQ GroupBy in C# from basic grouping to advanced overloads. It shows how a key selector splits a collection into sub-collections, how an element selector limits what each group contains, and how a result selector projects groups into a custom shape.
Reading notes#
- GroupBy groups related data by a chosen property and returns groups that can be iterated as collections.
- With a key selector like
books.GroupBy(b => b.Author), the result is anIEnumerable<IGrouping<string, Book>>. - An element selector can keep only part of each item, such as titles instead of full book objects.
- A result selector can project each group into a custom anonymous object with an author field and a collection of titles.
- With a result selector, the return type becomes
IEnumerable<TProjected>instead ofIEnumerable<IGrouping<TKey, TValue>>. - An equality comparer can control how keys are matched when grouping, such as ignoring casing differences with
StringComparer.CurrentCultureIgnoreCase. - When multiple values compare to the same group, LINQ uses the first encountered key as the group key.
- The page suggests cleaning and normalizing the data source when possible, and using a custom comparer only when grouping needs criteria other than reference equality.
