SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method Partition

    | Edit this page

    Partition<T>(IEnumerable<T>, Func<T, bool>)

    Partitions or splits a sequence in two using a predicate.

    Declaration
    public static (IEnumerable<T> True, IEnumerable<T> False) Partition<T>(this IEnumerable<T> source, Func<T, bool> predicate)
    Parameters
    Type Name Description
    IEnumerable<T> source

    The source sequence.

    Func<T, bool> predicate

    The predicate function.

    Returns
    Type Description
    (IEnumerable<T> True, IEnumerable<T> False)

    A tuple of elements satisfying the predicate and those that do not, respectively.

    Type Parameters
    Name Description
    T

    Type of source elements.

    Remarks

    This method executes immediately.

    Examples

    The following code example demonstrates how to partition a sequence using Partition.

    var sequence = Enumerable.Range(0, 10);
    
    // Partition a sequence
    var (evens, odds) = sequence
        .Partition(x => x % 2 == 0);
    
    Console.WriteLine(
        "evens: [" +
        string.Join(", ", evens) +
        "]");
    
    Console.WriteLine(
        "odds: [" +
        string.Join(", ", odds) +
        "]");
    
    // This code produces the following output:
    // evens: [0, 2, 4, 6, 8]
    // odds: [1, 3, 5, 7, 9]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    | Edit this page

    Partition<T, TResult>(IEnumerable<T>, Func<T, bool>, Func<IEnumerable<T>, IEnumerable<T>, TResult>)

    Partitions or splits a sequence in two using a predicate and then projects a result from the two.

    Declaration
    public static TResult Partition<T, TResult>(this IEnumerable<T> source, Func<T, bool> predicate, Func<IEnumerable<T>, IEnumerable<T>, TResult> resultSelector)
    Parameters
    Type Name Description
    IEnumerable<T> source

    The source sequence.

    Func<T, bool> predicate

    The predicate function.

    Func<IEnumerable<T>, IEnumerable<T>, TResult> resultSelector

    Function that projects the result from sequences of elements that satisfy the predicate and those that do not, respectively, passed as arguments.

    Returns
    Type Description
    TResult

    The return value from resultSelector.

    Type Parameters
    Name Description
    T

    Type of source elements.

    TResult

    Type of the result.

    Remarks

    This method executes immediately.

    Examples

    The following code example demonstrates how to partition a sequence using Partition.

    var sequence = Enumerable.Range(0, 10);
    
    // Partition a sequence
    var (evens, odds) = sequence
        .Partition(x => x % 2 == 0, ValueTuple.Create);
    
    Console.WriteLine(
        "evens: [" +
        string.Join(", ", evens) +
        "]");
    
    Console.WriteLine(
        "odds: [" +
        string.Join(", ", odds) +
        "]");
    
    // This code produces the following output:
    // evens: [0, 2, 4, 6, 8]
    // odds: [1, 3, 5, 7, 9]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source, predicate, or resultSelector is null.

    © SuperLinq Authors. All rights reserved.