Table of Contents

Method GroupBy

Namespace
MongoDB.Driver.Linq
Assembly
MongoDB.Driver.dll

GroupBy<TSource, TKey>(IMongoQueryable<TSource>, Expression<Func<TSource, TKey>>)

Groups the elements of a sequence according to a specified key selector function.

public static IMongoQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IMongoQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)

Parameters

source IMongoQueryable<TSource>

An IMongoQueryable<T> whose elements to group.

keySelector Expression<Func<TSource, TKey>>

A function to extract the key for each element.

Returns

IMongoQueryable<IGrouping<TKey, TSource>>

An IMongoQueryable<T> that has a type argument of IGrouping<TKey, TElement> and where each IGrouping<TKey, TElement> object contains a sequence of objects and a key.

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the key returned by the function represented in keySelector.

GroupBy<TSource, TKey, TResult>(IMongoQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TKey, IEnumerable<TSource>, TResult>>)

Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key.

public static IMongoQueryable<TResult> GroupBy<TSource, TKey, TResult>(this IMongoQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TKey, IEnumerable<TSource>, TResult>> resultSelector)

Parameters

source IMongoQueryable<TSource>

An IMongoQueryable<T> whose elements to group.

keySelector Expression<Func<TSource, TKey>>

A function to extract the key for each element.

resultSelector Expression<Func<TKey, IEnumerable<TSource>, TResult>>

A function to create a result value from each group.

Returns

IMongoQueryable<TResult>

An IMongoQueryable<T> that has a type argument of TResult and where each element represents a projection over a group and its key.

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the key returned by the function represented in keySelector.

TResult

The type of the result value returned by resultSelector.