QueryableExtensions.FirstOrDefaultAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
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>, 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>, 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>>, CancellationToken) |
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>)
Asynchronously returns the first element of a sequence, or a default value if the sequence contains no elements.
public static System.Threading.Tasks.Task<TSource> FirstOrDefaultAsync<TSource> (this System.Linq.IQueryable<TSource> source);
static member FirstOrDefaultAsync : System.Linq.IQueryable<'Source> -> System.Threading.Tasks.Task<'Source>
<Extension()>
Public Function FirstOrDefaultAsync(Of TSource) (source As IQueryable(Of TSource)) As Task(Of TSource)
Type Parameters
- TSource
The type of the elements of source
.
Parameters
- source
- IQueryable<TSource>
An IQueryable<T> to return the first element of.
Returns
A task that represents the asynchronous operation.
The task result contains default
( TSource
) if
source
is empty; otherwise, the first element in source
.
Exceptions
source
is
null
.
source
doesn't implement
IDbAsyncQueryProvider
.
Remarks
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.
Applies to
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.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public static System.Threading.Tasks.Task<TSource> FirstOrDefaultAsync<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member FirstOrDefaultAsync : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> System.Threading.Tasks.Task<'Source>
<Extension()>
Public Function FirstOrDefaultAsync(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As Task(Of TSource)
Type Parameters
- TSource
The type of the elements of source
.
Parameters
- source
- IQueryable<TSource>
An IQueryable<T> to return the first element of.
- predicate
- Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
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
.
- Attributes
Exceptions
source
or
predicate
is
null
.
source
doesn't implement
IDbAsyncQueryProvider
.
Remarks
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.
Applies to
FirstOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the first element of a sequence, or a default value if the sequence contains no elements.
public static System.Threading.Tasks.Task<TSource> FirstOrDefaultAsync<TSource> (this System.Linq.IQueryable<TSource> source, System.Threading.CancellationToken cancellationToken);
static member FirstOrDefaultAsync : System.Linq.IQueryable<'Source> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'Source>
Type Parameters
- TSource
The type of the elements of source
.
Parameters
- source
- IQueryable<TSource>
An IQueryable<T> to return the first element of.
- cancellationToken
- CancellationToken
A CancellationToken to observe while waiting for the task to complete.
Returns
A task that represents the asynchronous operation.
The task result contains default
( TSource
) if
source
is empty; otherwise, the first element in source
.
Exceptions
source
is
null
.
source
doesn't implement
IDbAsyncQueryProvider
.
Remarks
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.
Applies to
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.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public static System.Threading.Tasks.Task<TSource> FirstOrDefaultAsync<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate, System.Threading.CancellationToken cancellationToken);
static member FirstOrDefaultAsync : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'Source>
Type Parameters
- TSource
The type of the elements of source
.
Parameters
- source
- IQueryable<TSource>
An IQueryable<T> to return the first element of.
- predicate
- Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
- cancellationToken
- CancellationToken
A CancellationToken to observe while waiting for the task to complete.
Returns
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
.
- Attributes
Exceptions
source
or
predicate
is
null
.
source
has more than one element.
Remarks
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.
Applies to
Entity Framework