QueryableExtensions.SingleAsync Method

 

Asynchronously returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

Namespace:   System.Data.Entity
Assembly:  EntityFramework (in EntityFramework.dll)

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticSingleAsync<TSource>(IQueryable<TSource>)

Asynchronously returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

System_CAPS_pubmethodSystem_CAPS_staticSingleAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

System_CAPS_pubmethodSystem_CAPS_staticSingleAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, Boolean>>)

Asynchronously returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

System_CAPS_pubmethodSystem_CAPS_staticSingleAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, Boolean>>, CancellationToken)

Asynchronously returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.


QueryableExtensions.SingleAsync<TSource> Method (IQueryable<TSource>)

Asynchronously returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

public static Task<TSource> SingleAsync<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.

Type Parameters

TSource

The type of the elements of source.

Exception Condition
ArgumentNullException

source is null .

InvalidOperationException

source does not implement IDbAsyncQueryProvider .

InvalidOperationException

The source sequence is empty.

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.SingleAsync<TSource> Method (IQueryable<TSource>, CancellationToken)

Asynchronously returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

public static Task<TSource> SingleAsync<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.

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.

InvalidOperationException

The source sequence is empty.

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.SingleAsync<TSource> Method (IQueryable<TSource>, Expression<Func<TSource, Boolean>>)

Asynchronously returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

[SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public static Task<TSource> SingleAsync<TSource>(
	this IQueryable<TSource> source,
	Expression<Func<TSource, bool>> predicate
)

Parameters

source
Type: System.Linq.IQueryable<TSource>

An IQueryable<T> to return the 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.

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

No element satisfies the condition in predicate .

InvalidOperationException

More than one element satisfies the condition in predicate .

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.SingleAsync<TSource> Method (IQueryable<TSource>, Expression<Func<TSource, Boolean>>, CancellationToken)

Asynchronously returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

[SuppressMessageAttribute("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
[SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public static Task<TSource> SingleAsync<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.

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

No element satisfies the condition in predicate .

InvalidOperationException

More than one element satisfies the condition in predicate .

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.