Table of Contents

Method SingleOrDefaultAsync

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

SingleOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)

Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

public static Task<TSource> SingleOrDefaultAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)

Parameters

source IQueryable<TSource>

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

cancellationToken CancellationToken

The cancellation token.

Returns

Task<TSource>

The single element of the input sequence, or default(TSource) if the sequence contains no elements.

Type Parameters

TSource

The type of the elements of source.

SingleOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)

Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.

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

Parameters

source IQueryable<TSource>

An IQueryable<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, or default(TSource) if no such element is found.

Type Parameters

TSource

The type of the elements of source.