Op Englesch liesen Editéieren

Deelen iwwer


AsyncEnumerable.SingleOrDefaultAsync Method

Definition

Overloads

SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, CancellationToken)

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>(IAsyncEnumerable<TSource>, Func<TSource,Boolean>, CancellationToken)

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>(IAsyncEnumerable<TSource>, Func<TSource,CancellationToken,ValueTask<Boolean>>, CancellationToken)

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>(IAsyncEnumerable<TSource>, TSource, CancellationToken)

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>(IAsyncEnumerable<TSource>, Func<TSource,Boolean>, TSource, CancellationToken)

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>(IAsyncEnumerable<TSource>, Func<TSource,CancellationToken,ValueTask<Boolean>>, TSource, CancellationToken)

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>(IAsyncEnumerable<TSource>, CancellationToken)

Source:
SingleAsync.cs

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.

C#
public static System.Threading.Tasks.ValueTask<TSource?> SingleOrDefaultAsync<TSource>(this System.Collections.Generic.IAsyncEnumerable<TSource> source, System.Threading.CancellationToken cancellationToken = default);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IAsyncEnumerable<TSource>

An IAsyncEnumerable<T> to return the single element of.

cancellationToken
CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

ValueTask<TSource>

The single element of the input sequence, or the default value of TSource if the sequence contains no elements.

Exceptions

source is null.

The source sequence contains more than one element. (via the returned task).

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, Func<TSource,Boolean>, CancellationToken)

Source:
SingleAsync.cs

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.

C#
public static System.Threading.Tasks.ValueTask<TSource?> SingleOrDefaultAsync<TSource>(this System.Collections.Generic.IAsyncEnumerable<TSource> source, Func<TSource,bool> predicate, System.Threading.CancellationToken cancellationToken = default);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IAsyncEnumerable<TSource>

An IAsyncEnumerable<T> to return the single element of.

predicate
Func<TSource,Boolean>

A function to test an element for a condition.

cancellationToken
CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

ValueTask<TSource>

The single element of the input sequence that satisfies the condition, or the default value of TSource if no such element is found.

Exceptions

predicate is null.

The input sequence contains more than one element.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, Func<TSource,CancellationToken,ValueTask<Boolean>>, CancellationToken)

Source:
SingleAsync.cs

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.

C#
public static System.Threading.Tasks.ValueTask<TSource?> SingleOrDefaultAsync<TSource>(this System.Collections.Generic.IAsyncEnumerable<TSource> source, Func<TSource,System.Threading.CancellationToken,System.Threading.Tasks.ValueTask<bool>> predicate, System.Threading.CancellationToken cancellationToken = default);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IAsyncEnumerable<TSource>

An IAsyncEnumerable<T> to return the single element of.

predicate
Func<TSource,CancellationToken,ValueTask<Boolean>>

A function to test an element for a condition.

cancellationToken
CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

ValueTask<TSource>

The single element of the input sequence that satisfies the condition, or the default value of TSource if no such element is found.

Exceptions

predicate is null.

The input sequence contains more than one element.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, TSource, CancellationToken)

Source:
SingleAsync.cs

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.

C#
public static System.Threading.Tasks.ValueTask<TSource> SingleOrDefaultAsync<TSource>(this System.Collections.Generic.IAsyncEnumerable<TSource> source, TSource defaultValue, System.Threading.CancellationToken cancellationToken = default);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IAsyncEnumerable<TSource>

An IAsyncEnumerable<T> to return the single element of.

defaultValue
TSource

The default value to return if the sequence is empty.

cancellationToken
CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

ValueTask<TSource>

The single element of the input sequence, or defaultValue if the sequence contains no elements.

Exceptions

source is null.

The input sequence contains more than one element.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, Func<TSource,Boolean>, TSource, CancellationToken)

Source:
SingleAsync.cs

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.

C#
public static System.Threading.Tasks.ValueTask<TSource> SingleOrDefaultAsync<TSource>(this System.Collections.Generic.IAsyncEnumerable<TSource> source, Func<TSource,bool> predicate, TSource defaultValue, System.Threading.CancellationToken cancellationToken = default);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IAsyncEnumerable<TSource>

An IAsyncEnumerable<T> to return a single element from.

predicate
Func<TSource,Boolean>

A function to test an element for a condition.

defaultValue
TSource

The default value to return if the sequence is empty.

cancellationToken
CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

ValueTask<TSource>

The single element of the input sequence that satisfies the condition, or defaultValue if no such element is found.

Exceptions

predicate is null.

More than one element satisfies the condition in predicate.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, Func<TSource,CancellationToken,ValueTask<Boolean>>, TSource, CancellationToken)

Source:
SingleAsync.cs

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.

C#
public static System.Threading.Tasks.ValueTask<TSource> SingleOrDefaultAsync<TSource>(this System.Collections.Generic.IAsyncEnumerable<TSource> source, Func<TSource,System.Threading.CancellationToken,System.Threading.Tasks.ValueTask<bool>> predicate, TSource defaultValue, System.Threading.CancellationToken cancellationToken = default);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IAsyncEnumerable<TSource>

An IAsyncEnumerable<T> to return a single element from.

predicate
Func<TSource,CancellationToken,ValueTask<Boolean>>

A function to test an element for a condition.

defaultValue
TSource

The default value to return if the sequence is empty.

cancellationToken
CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

ValueTask<TSource>

The single element of the input sequence that satisfies the condition, or defaultValue if no such element is found.

Exceptions

predicate is null.

More than one element satisfies the condition in predicate.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided), 10
.NET Standard 2.0 (package-provided)