SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method If

    | Edit this page

    If<TResult>(Func<bool>, IEnumerable<TResult>)

    Returns an enumerable sequence if the evaluation result of the given condition is true, otherwise returns an empty sequence.

    Declaration
    public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource)
    Parameters
    Type Name Description
    Func<bool> condition

    Condition to evaluate.

    IEnumerable<TResult> thenSource

    Sequence to return in case the condition evaluates true.

    Returns
    Type Description
    IEnumerable<TResult>

    The given input sequence if the condition evaluates true; otherwise, an empty sequence.

    Type Parameters
    Name Description
    TResult

    Result sequence element type.

    Remarks

    condition is not evaluated until enumeration. If the value is true, then thenSource is enumerated. Otherwise, the sequence will be empty.

    Examples

    The following code example demonstrates how to selectively enumerate a sequence using If.

    var sequence = Enumerable.Range(1, 5);
    
    // Use a function to select which sequence to return values from.
    var selector = true;
    var result = SuperEnumerable
        .If(
            () => selector,
            sequence);
    
    Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
    selector = false;
    Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
    selector = true;
    Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
    
    // This code produces the following output:
    // Selector: True; result.Count(): 5.
    // Selector: False; result.Count(): 0.
    // Selector: True; result.Count(): 5.
    
    Exceptions
    Type Condition
    ArgumentNullException

    condition or thenSource is null.

    | Edit this page

    If<TResult>(Func<bool>, IEnumerable<TResult>, IEnumerable<TResult>)

    Returns an enumerable sequence based on the evaluation result of the given condition.

    Declaration
    public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource, IEnumerable<TResult> elseSource)
    Parameters
    Type Name Description
    Func<bool> condition

    Condition to evaluate.

    IEnumerable<TResult> thenSource

    Sequence to return in case the condition evaluates true.

    IEnumerable<TResult> elseSource

    Sequence to return in case the condition evaluates false.

    Returns
    Type Description
    IEnumerable<TResult>

    If the condition evaluates true, then the sequence represented by thenSource; otherwise the sequence represented by elseSource.

    Type Parameters
    Name Description
    TResult

    Result sequence element type.

    Remarks

    condition is not evaluated until enumeration. If the value is true, then thenSource is enumerated. Otherwise, elseSource will be enumerated.

    Examples

    The following code example demonstrates how to selectively enumerate a sequence using If.

    var sequence1 = Enumerable.Range(1, 5);
    var sequence2 = Enumerable.Range(1, 10);
    
    // Use a function to select which sequence to return values from.
    var selector = true;
    var result = SuperEnumerable
        .If(
            () => selector,
            sequence1,
            sequence2);
    
    Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
    selector = false;
    Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
    selector = true;
    Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
    
    // This code produces the following output:
    // Selector: True; result.Count(): 5.
    // Selector: False; result.Count(): 10.
    // Selector: True; result.Count(): 5.
    
    Exceptions
    Type Condition
    ArgumentNullException

    condition, thenSource, or elseSource is null.

    © SuperLinq Authors. All rights reserved.