Enumerable.CountBy<TSource,TKey> Method

Definition

Returns the count of elements in the source sequence grouped by key.

public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,int>> CountBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? keyComparer = default);

Type Parameters

TSource

The type of elements of source.

TKey

The type of the key returned by keySelector.

Parameters

source
IEnumerable<TSource>

A sequence that contains elements to be counted.

keySelector
Func<TSource,TKey>

A function to extract the key for each element.

keyComparer
IEqualityComparer<TKey>

An IEqualityComparer<T> to compare keys with.

Returns

An enumerable containing the frequencies of each key occurrence in source.

Applies to

Product Versions
.NET 9