Table of Contents

Method FirstOrDefaultAsync

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

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

Returns the first element of a sequence, or a default value if the sequence contains no elements.

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

Parameters

source IMongoQueryable<TSource>

The IMongoQueryable<T> to return the first element of.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<TSource>

default(TSource) if source is empty; otherwise, the first element in source.

Type Parameters

TSource

The type of the elements of source.

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

Returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.

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

Parameters

source IMongoQueryable<TSource>

An IMongoQueryable<T> to return an element from.

predicate Expression<Func<TSource, bool>>

A function to test each element for a condition.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<TSource>

default(TSource) if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate.

Type Parameters

TSource

The type of the elements of source.