QueryableExtensions.FirstOrDefaultAsync Method
Asynchronously returns the first element of a sequence, or a default value if the sequence contains no elements.
Assembly: EntityFramework (in EntityFramework.dll)
Name | Description | |
---|---|---|
![]() ![]() | FirstOrDefaultAsync<TSource>(IQueryable<TSource>) | Asynchronously returns the first element of a sequence, or a default value if the sequence contains no elements. |
![]() ![]() | FirstOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken) | Asynchronously returns the first element of a sequence, or a default value if the sequence contains no elements. |
![]() ![]() | FirstOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, Boolean>>) | Asynchronously returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found. |
![]() ![]() | FirstOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, Boolean>>, CancellationToken) | Asynchronously returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found. |
QueryableExtensions.FirstOrDefaultAsync<TSource> Method (IQueryable<TSource>)
Asynchronously 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 )
Parameters
- source
-
Type:
System.Linq.IQueryable<TSource>
An IQueryable<T> to return the first element of.
Return Value
Type: System.Threading.Tasks.Task<TSource>A task that represents the asynchronous operation. The task result contains default ( TSource ) if source is empty; otherwise, the first element in source.
Type Parameters
- TSource
The type of the elements of source.
Exception | Condition |
---|---|
ArgumentNullException | source is null . |
InvalidOperationException | source does not implement IDbAsyncQueryProvider . |
Multiple active operations on the same context instance are not supported. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context.
QueryableExtensions.FirstOrDefaultAsync<TSource> Method (IQueryable<TSource>, CancellationToken)
Asynchronously 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 )
Parameters
- source
-
Type:
System.Linq.IQueryable<TSource>
An IQueryable<T> to return the first element of.
- cancellationToken
-
Type:
System.Threading.CancellationToken
A CancellationToken to observe while waiting for the task to complete.
Return Value
Type: System.Threading.Tasks.Task<TSource>A task that represents the asynchronous operation. The task result contains default ( TSource ) if source is empty; otherwise, the first element in source.
Type Parameters
- TSource
The type of the elements of source.
Exception | Condition |
---|---|
ArgumentNullException | source is null . |
InvalidOperationException | source does not implement IDbAsyncQueryProvider . |
Multiple active operations on the same context instance are not supported. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context.
QueryableExtensions.FirstOrDefaultAsync<TSource> Method (IQueryable<TSource>, Expression<Func<TSource, Boolean>>)
Asynchronously returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.
[SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] public static Task<TSource> FirstOrDefaultAsync<TSource>( this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate )
Parameters
- source
-
Type:
System.Linq.IQueryable<TSource>
An IQueryable<T> to return the first element of.
- predicate
-
Type:
System.Linq.Expressions.Expression<Func<TSource, Boolean>>
A function to test each element for a condition.
Return Value
Type: System.Threading.Tasks.Task<TSource>A task that represents the asynchronous operation. The task result contains 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.
Exception | Condition |
---|---|
ArgumentNullException | source or predicate is null . |
InvalidOperationException | source does not implement IDbAsyncQueryProvider . |
Multiple active operations on the same context instance are not supported. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context.
QueryableExtensions.FirstOrDefaultAsync<TSource> Method (IQueryable<TSource>, Expression<Func<TSource, Boolean>>, CancellationToken)
Asynchronously returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.
[SuppressMessageAttribute("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] public static Task<TSource> FirstOrDefaultAsync<TSource>( this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken )
Parameters
- source
-
Type:
System.Linq.IQueryable<TSource>
An IQueryable<T> to return the first element of.
- predicate
-
Type:
System.Linq.Expressions.Expression<Func<TSource, Boolean>>
A function to test each element for a condition.
- cancellationToken
-
Type:
System.Threading.CancellationToken
A CancellationToken to observe while waiting for the task to complete.
Return Value
Type: System.Threading.Tasks.Task<TSource>A task that represents the asynchronous operation. The task result contains 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.
Exception | Condition |
---|---|
ArgumentNullException | source or predicate is null . |
InvalidOperationException | source does not implement IDbAsyncQueryProvider . |
InvalidOperationException | source has more than one element. |
Multiple active operations on the same context instance are not supported. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context.