Queryable.CountBy<TSource,TKey> Method

Definition

Returns the count of each element from a sequence according to a specified key selector function.

C#
public static System.Linq.IQueryable<System.Collections.Generic.KeyValuePair<TKey,int>> CountBy<TSource,TKey>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer = default);

Type Parameters

TSource

The type of the elements of source.

TKey

The type of key to distinguish elements by.

Parameters

source
IQueryable<TSource>

The sequence to count elements from.

keySelector
Expression<Func<TSource,TKey>>

A function to extract the key for each element.

comparer
IEqualityComparer<TKey>

An IEqualityComparer<T> to compare keys.

Returns

An IQueryable<T> that contains count for each distinct elements from the source sequence as a KeyValuePair<TKey,TValue> object.

Exceptions

source is null.

Applies to

Product Versions
.NET 9