GroupBy<TKey> method

Enumerable<IGrouping<TKey, T>> GroupBy <TKey>(Selector<T, TKey> keySelector, { EqualityComparer<TKey> keyComparer })

Groups the elements in the enumerable by a key.

After applying the GroupBy method to an enumerable, the resulting enumerable will be a series of groups of elements. Each group will consist of all elements in the source enumerable that share a common key as defined by passing the element to the keySelector function.

Optionally, an EqualityComparer can be supplied to handle key comparisons. If one is provided, the GroupBy method will use the comparer and hasher properties in order to determine equivalency. If omitted, GroupBy will resort to strict equivalency (i.e. checking if (value == element)).

(For the GroupBy method, only the comparer and hasher properties of the EqualityComparer need be supplied.)

The resulting enumerable will consist of a series of IGrouping constructs that contain the elements. If no two elements in the enumerable share a common key, the resulting enumerable will consist of IGrouping objects (each containing a single element) of the same length as the source enumerable.

Implementation

Enumerable<IGrouping<TKey, T>> GroupBy<TKey>(Selector<T, TKey> keySelector,
    {EqualityComparer<TKey> keyComparer}) {
  assert(keySelector != null);
  return GroupByEnumerable<T, TKey>(this, keySelector, keyComparer);
}