SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    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 TCardinality and either the single value of T in the sequence or its default value.

    Type Parameters
    Name Description
    T

    The type of the elements of source.

    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

    source is null

    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 resultSelector if the sequence has zero elements.

    TCardinality one

    The value that is passed as the first argument to resultSelector if the sequence has a single element only.

    TCardinality many

    The value that is passed as the first argument to resultSelector if the sequence has two or more elements.

    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 TResult.

    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 resultSelector.

    Type Parameters
    Name Description
    T

    The type of the elements of source.

    TCardinality

    The type that expresses cardinality.

    TResult

    The type of the result value returned by the resultSelector function.

    Remarks

    This operator uses immediate execution, but never consumes more than two elements from the sequence.

    Exceptions
    Type Condition
    ArgumentNullException

    source is null

    ArgumentNullException

    resultSelector is null

    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 TSource.

    Type Parameters
    Name Description
    TSource

    The type of the elements of source.

    © SuperLinq Authors. All rights reserved.