Table of Contents

Method SingleAsync

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

SingleAsync<TSource>(IMongoQueryable<TSource>, CancellationToken)

Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

public static Task<TSource> SingleAsync<TSource>(this IMongoQueryable<TSource> source, CancellationToken cancellationToken = default)

Parameters

source IMongoQueryable<TSource>

An IMongoQueryable<T> to return the single element of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<TSource>

The single element of the input sequence.

Type Parameters

TSource

The type of the elements of source.

SingleAsync<TSource>(IMongoQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)

Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

public static Task<TSource> SingleAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)

Parameters

source IMongoQueryable<TSource>

An IMongoQueryable<T> to return a single element from.

predicate Expression<Func<TSource, bool>>

A function to test an element for a condition.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<TSource>

The single element of the input sequence that satisfies the condition in predicate.

Type Parameters

TSource

The type of the elements of source.