Method FirstOrDefaultAsync
FirstOrDefaultAsync<TSource>(IQueryable<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 IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
sourceIQueryable<TSource>The IQueryable<T> to return the first element of.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<TSource>
default(
TSource) ifsourceis empty; otherwise, the first element insource.
Type Parameters
TSourceThe type of the elements of
source.
FirstOrDefaultAsync<TSource>(IQueryable<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 IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
sourceIQueryable<TSource>An IQueryable<T> to return an element from.
predicateExpression<Func<TSource, bool>>A function to test each element for a condition.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<TSource>
default(
TSource) ifsourceis empty or if no element passes the test specified bypredicate; otherwise, the first element insourcethat passes the test specified bypredicate.
Type Parameters
TSourceThe type of the elements of
source.