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