SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method TrySingle

    | Edit this page

    TrySingle<T, TCardinality>(IEnumerable<T>, TCardinality, TCardinality, TCardinality)

    Returns a tuple with the cardinality of the sequence and the single element in the sequence if it contains exactly one element. similar to Single<TSource>(IEnumerable<TSource>).

    Declaration
    public static (TCardinality Cardinality, T? Value) TrySingle<T, TCardinality>(this IEnumerable<T> source, TCardinality zero, TCardinality one, TCardinality many)
    Parameters
    Type Name Description
    IEnumerable<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.

    Returns
    Type Description
    (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.

    Examples

    The following code example demonstrates how to evaluate the cardinality of a sequence using TrySingle.

    // Determine cardinality of sequence
    var result = Enumerable.Range(1, 0).TrySingle("none", "one", "many");
    Console.WriteLine(result.ToString());
    
    result = Enumerable.Range(1, 1).TrySingle("none", "one", "many");
    Console.WriteLine(result.ToString());
    
    result = Enumerable.Range(1, 10).TrySingle("none", "one", "many");
    Console.WriteLine(result.ToString());
    
    // This code produces the following output:
    // (none, 0)
    // (one, 1)
    // (many, 0)
    
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    | Edit this page

    TrySingle<T, TCardinality, TResult>(IEnumerable<T>, TCardinality, TCardinality, TCardinality, Func<TCardinality, T?, TResult>)

    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 TResult TrySingle<T, TCardinality, TResult>(this IEnumerable<T> source, TCardinality zero, TCardinality one, TCardinality many, Func<TCardinality, T?, TResult> resultSelector)
    Parameters
    Type Name Description
    IEnumerable<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.

    Returns
    Type Description
    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.

    Examples

    The following code example demonstrates how to evaluate the cardinality of a sequence using TrySingle.

    // Determine cardinality of sequence
    var result = Enumerable.Range(10, 0).TrySingle(0, 1, 2, (count, one) => count switch { 0 => "no elements", 1 => $"single({one})", 2 => "many elements" });
    Console.WriteLine(result);
    
    result = Enumerable.Range(10, 1).TrySingle(0, 1, 2, (count, one) => count switch { 0 => "no elements", 1 => $"single({one})", 2 => "many elements" });
    Console.WriteLine(result);
    
    result = Enumerable.Range(10, 10).TrySingle(0, 1, 2, (count, one) => count switch { 0 => "no elements", 1 => $"single({one})", 2 => "many elements" });
    Console.WriteLine(result);
    
    // This code produces the following output:
    // no elements
    // single(10)
    // many elements
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or resultSelector is null.

    TrySingle<TSource>(IEnumerable<TSource>)

    Returns the single element in the sequence if it contains exactly one element. Similar to SingleOrDefault<TSource>(IEnumerable<TSource>).

    Declaration
    public static TSource? TrySingle<TSource>(this IEnumerable<TSource> source)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    Returns
    Type Description
    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.