In LINQ, grouping operators pick the elements of the sequence or collection which contains common attributes and serve them in a group. Or in other words, we can say that the grouping operator returns the group of elements based on the given key. This group is held in a special type of collection, which implements the IGrouping<TKey, TElement> interface, where TKey is the key through which the group is created and TElement is the collection of elements which peers with the grouping key value. For example, a sequence contains 6 elements, i.e, Abc, cvd, Abc, Abc, ert, Por, now we want a group in which contain all the Abc present in that sequence. So Abc is the key through which we create another group that contains 3 Abc. As shown in the below image:
The Standard Query Operator contains two different types of grouping operators:
The ToLookup operator is used to put the element in a LookUp<TKey, TElement> based on a key selector function. Here, LookUp<TKey, TElement> is a one-to-many dictionary. The working of the ToLookup operator is quite similar to the GroupBy operator.
- It does not support query syntax in C# and VB.Net languages.
- It support method syntax in both C# and VB.Net languages.
- It only present in Enumerable class.
- It is overloaded in four different types.
- It throw ArgumentNullException if the source or keySelector is null.
- It is implemented by using immediate execution.
Group By Gender: Female Employee Name: Anjita Employee Name: Soniya Employee Name: Supriya Employee Name: Anju Group By Gender: Male Employee Name: Rohit Employee Name: Anil
- LINQ | Grouping Operator | GroupBy
- LINQ | Filtering Operator | OfType
- LINQ | Set Operator | Except
- LINQ | Element Operator | Last
- LINQ | Projection Operator | Select
- LINQ | Projection Operator | SelectMany
- LINQ | Filtering Operator | where
- LINQ | Sorting Operator | OrderBy
- LINQ | Sorting Operator | OrderByDescending
- LINQ | Sorting Operator | Reverse
- LINQ | Sorting Operator | ThenBy
- LINQ | Equality Operator | SequenceEqual
- LINQ | Set Operator | Distinct
- LINQ | Concatenation Operator | Concat
- LINQ | Set Operator | Intersect
- LINQ | Set Operator | Union
- LINQ | Element Operator | First
- LINQ | Element Operator | FirstOrDefault
- LINQ | Element Operator | LastOrDefault
- LINQ | Element Operator | Single
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.