SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method AggregateBy

    | Edit this page

    AggregateBy<TSource, TKey, TAccumulate>(IEnumerable<TSource>, Func<TSource, TKey>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>, IEqualityComparer<TKey>?)

    Applies a key-generating function to each element of a sequence and returns an aggregate value for each key. An additional argument specifies a comparer to use for testing equivalence of keys.

    Declaration
    public static IEnumerable<KeyValuePair<TKey, TAccumulate>> AggregateBy<TSource, TKey, TAccumulate>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? comparer = null) where TKey : notnull
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    Source sequence.

    Func<TSource, TKey> keySelector

    Function that transforms each item of source sequence into a key to be compared against the others.

    TAccumulate seed

    The initial accumulator value for each key-group.

    Func<TAccumulate, TSource, TAccumulate> func

    An accumulator function to be invoked on each element. The accumulator value is tracked separately for each key.

    IEqualityComparer<TKey> comparer

    The equality comparer to use to determine whether or not keys are equal. If null, the default equality comparer for TKey is used.

    Returns
    Type Description
    IEnumerable<KeyValuePair<TKey, TAccumulate>>

    A sequence of unique keys and their accumulated value.

    Type Parameters
    Name Description
    TSource

    Type of the elements of the source sequence.

    TKey

    Type of the projected element.

    TAccumulate

    Type of the accumulator value.

    Remarks

    This method is implemented by using deferred execution. The operator will be executed in it's entirety immediately when the sequence is first enumerated.

    Examples

    The following code example demonstrates how to aggregate values by key in a sequence, using AggregateBy.

    var sequence = Enumerable.Range(1, 19);
                
    // Aggregate elements in a sequence grouped by key
    var result = sequence
        .AggregateBy(
            x => x % 3,
            0,
            (acc, e) => acc + e);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [[1, 70], [2, 57], [0, 63]]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source, keySelector, or func is null.

    | Edit this page

    AggregateBy<TSource, TKey, TAccumulate>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TKey, TAccumulate>, Func<TAccumulate, TSource, TAccumulate>, IEqualityComparer<TKey>?)

    Applies a key-generating function to each element of a sequence and returns an aggregate value for each key. An additional argument specifies a comparer to use for testing equivalence of keys.

    Declaration
    public static IEnumerable<KeyValuePair<TKey, TAccumulate>> AggregateBy<TSource, TKey, TAccumulate>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TAccumulate> seedSelector, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? comparer = null) where TKey : notnull
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    Source sequence.

    Func<TSource, TKey> keySelector

    Function that transforms each item of source sequence into a key to be compared against the others.

    Func<TKey, TAccumulate> seedSelector

    A function that returns the initial seed for each key.

    Func<TAccumulate, TSource, TAccumulate> func

    An accumulator function to be invoked on each element. The accumulator value is tracked separately for each key.

    IEqualityComparer<TKey> comparer

    The equality comparer to use to determine whether or not keys are equal. If null, the default equality comparer for TKey is used.

    Returns
    Type Description
    IEnumerable<KeyValuePair<TKey, TAccumulate>>

    A sequence of unique keys and their accumulated value.

    Type Parameters
    Name Description
    TSource

    Type of the elements of the source sequence.

    TKey

    Type of the projected element.

    TAccumulate

    Type of the accumulator value.

    Remarks

    This method is implemented by using deferred execution. The operator will be executed in it's entirety immediately when the sequence is first enumerated.

    Examples

    The following code example demonstrates how to aggregate values by key in a sequence, using AggregateBy.

    var sequence = Enumerable.Range(1, 19);
                
    // Aggregate elements in a sequence grouped by key
    var result = sequence
        .AggregateBy(
            x => x % 3,
            k => k * 1_000,
            (acc, e) => acc + e);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [[1, 1070], [2, 2057], [0, 63]]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source, seedSelector, keySelector, or func is null.

    © SuperLinq Authors. All rights reserved.