QueryableExtensions.SingleOrDefaultAsync Method
Asynchronously 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.
Assembly: EntityFramework (in EntityFramework.dll)
Name | Description | |
---|---|---|
![]() ![]() | SingleOrDefaultAsync<TSource>(IQueryable<TSource>) | Asynchronously 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. |
![]() ![]() | SingleOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken) | Asynchronously 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. |
![]() ![]() | SingleOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, Boolean>>) | Asynchronously 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. |
![]() ![]() | SingleOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, Boolean>>, CancellationToken) | Asynchronously 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. |
QueryableExtensions.SingleOrDefaultAsync<TSource> Method (IQueryable<TSource>)
Asynchronously 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 )
Parameters
- source
-
Type:
System.Linq.IQueryable<TSource>
An IQueryable<T> to return the single element of.
Return Value
Type: System.Threading.Tasks.Task<TSource>A task that represents the asynchronous operation. The task result contains 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.
Exception | Condition |
---|---|
ArgumentNullException | source 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.
QueryableExtensions.SingleOrDefaultAsync<TSource> Method (IQueryable<TSource>, CancellationToken)
Asynchronously 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 )
Parameters
- source
-
Type:
System.Linq.IQueryable<TSource>
An IQueryable<T> to return the single 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 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.
Exception | Condition |
---|---|
ArgumentNullException | source 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.
QueryableExtensions.SingleOrDefaultAsync<TSource> Method (IQueryable<TSource>, Expression<Func<TSource, Boolean>>)
Asynchronously 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.
[SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] public static Task<TSource> SingleOrDefaultAsync<TSource>( this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate )
Parameters
- source
-
Type:
System.Linq.IQueryable<TSource>
An IQueryable<T> to return the single element of.
- predicate
-
Type:
System.Linq.Expressions.Expression<Func<TSource, Boolean>>
A function to test an element for a condition.
Return Value
Type: System.Threading.Tasks.Task<TSource>A task that represents the asynchronous operation. The task result contains 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.
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.SingleOrDefaultAsync<TSource> Method (IQueryable<TSource>, Expression<Func<TSource, Boolean>>, CancellationToken)
Asynchronously 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.
[SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] [SuppressMessageAttribute("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] public static Task<TSource> SingleOrDefaultAsync<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 single element of.
- predicate
-
Type:
System.Linq.Expressions.Expression<Func<TSource, Boolean>>
A function to test an 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 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.
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.