Method TrySingle
TrySingle<T, TCardinality>(IAsyncEnumerable<T>, TCardinality, TCardinality, TCardinality, CancellationToken)
Returns a tuple with the cardinality of the sequence and the single element in the sequence if it contains exactly one element. similar to SingleAsync<TSource>(IAsyncEnumerable<TSource>, CancellationToken).
Declaration
public static ValueTask<(TCardinality Cardinality, T? Value)> TrySingle<T, TCardinality>(this IAsyncEnumerable<T> source, TCardinality zero, TCardinality one, TCardinality many, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IAsyncEnumerable<T> | source | The source sequence. |
| TCardinality | zero | The value that is returned in the tuple if the sequence has zero elements. |
| TCardinality | one | The value that is returned in the tuple if the sequence has a single element only. |
| TCardinality | many | The value that is returned in the tuple if the sequence has two or more elements. |
| CancellationToken | cancellationToken | The optional cancellation token to be used for cancelling the sequence at any time. |
Returns
| Type | Description |
|---|---|
| ValueTask<(TCardinality Cardinality, T Value)> | A tuple containing the identified |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the elements of |
| TCardinality | The type that expresses cardinality. |
Remarks
This operator uses immediate execution, but never consumes more than two elements from the sequence.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
TrySingle<T, TCardinality, TResult>(IAsyncEnumerable<T>, TCardinality, TCardinality, TCardinality, Func<TCardinality, T?, TResult>, CancellationToken)
Returns a result projected from the the cardinality of the sequence and the single element in the sequence if it contains exactly one element.
Declaration
public static ValueTask<TResult> TrySingle<T, TCardinality, TResult>(this IAsyncEnumerable<T> source, TCardinality zero, TCardinality one, TCardinality many, Func<TCardinality, T?, TResult> resultSelector, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IAsyncEnumerable<T> | source | The source sequence. |
| TCardinality | zero | The value that is passed as the first argument to
|
| TCardinality | one | The value that is passed as the first argument to
|
| TCardinality | many | The value that is passed as the first argument to
|
| Func<TCardinality, T, TResult> | resultSelector | A function that receives the cardinality and, if the
sequence has just one element, the value of that element as
argument and projects a resulting value of type
|
| CancellationToken | cancellationToken | The optional cancellation token to be used for cancelling the sequence at any time. |
Returns
| Type | Description |
|---|---|
| ValueTask<TResult> | The value returned by |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the elements of |
| TCardinality | The type that expresses cardinality. |
| TResult | The type of the result value returned by the
|
Remarks
This operator uses immediate execution, but never consumes more than two elements from the sequence.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentNullException |
|
TrySingle<TSource>(IAsyncEnumerable<TSource>, CancellationToken)
Returns the single element in the sequence if it contains exactly one element. Similar to SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, CancellationToken).
Declaration
public static ValueTask<TSource?> TrySingle<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IAsyncEnumerable<TSource> | source | The source sequence. |
| CancellationToken | cancellationToken | The optional cancellation token to be used for cancelling the sequence at any time. |
Returns
| Type | Description |
|---|---|
| ValueTask<TSource> | The single element or the default value of |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements of |