Method SingleOrDefaultAsync
| MongoDB CSharp Driver API Reference (original) (raw)
Namespace
Assembly
MongoDB.Driver.dll
SingleOrDefaultAsync(IQueryable, 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
An IQueryable to return the single element of.
cancellationToken
CancellationToken
The cancellation token.
Returns
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(IQueryable, 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
An IQueryable 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
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
.