Sdílet prostřednictvím


Enumerable.AggregateBy Metoda

Definice

Přetížení

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

Zdroj:
AggregateBy.cs
Zdroj:
AggregateBy.cs
Zdroj:
AggregateBy.cs

Použije funkci akumulátoru na sekvenci, seskupí výsledky podle klíče.

public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TAccumulate>> AggregateBy<TSource,TKey,TAccumulate>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TKey,TAccumulate> seedSelector, Func<TAccumulate,TSource,TAccumulate> func, System.Collections.Generic.IEqualityComparer<TKey>? keyComparer = default);
static member AggregateBy : seq<'Source> * Func<'Source, 'Key> * Func<'Key, 'Accumulate> * Func<'Accumulate, 'Source, 'Accumulate> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<System.Collections.Generic.KeyValuePair<'Key, 'Accumulate>>
<Extension()>
Public Function AggregateBy(Of TSource, TKey, TAccumulate) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), seedSelector As Func(Of TKey, TAccumulate), func As Func(Of TAccumulate, TSource, TAccumulate), Optional keyComparer As IEqualityComparer(Of TKey) = Nothing) As IEnumerable(Of KeyValuePair(Of TKey, TAccumulate))

Parametry typu

TSource

Typ prvků .source

TKey

Typ klíče vráceného řetězcem keySelector.

TAccumulate

Typ akumulátoru.

Parametry

source
IEnumerable<TSource>

Agregace IEnumerable<T> .

keySelector
Func<TSource,TKey>

Funkce, která extrahuje klíč pro každý prvek.

seedSelector
Func<TKey,TAccumulate>

Továrna pro počáteční hodnotu akumulátoru.

func
Func<TAccumulate,TSource,TAccumulate>

Akumulátorová funkce, která má být vyvolána na každém prvku.

keyComparer
IEqualityComparer<TKey>

Porovnávat IEqualityComparer<T> klíče s.

Návraty

IEnumerable<KeyValuePair<TKey,TAccumulate>>

Výčet obsahující agregace odpovídající jednotlivým klíčům odvozeným od source.

Příklady

Následující příklad ukazuje použití AggregateBy selektoru počátečních hodnot pro výpočet více hodnot na klíč.

public static void AggregateBySeedSelectorExample()
{
    (string Name, string Department, decimal Salary)[] employees =
    {
        ("Ali", "HR", 45000),
        ("Samer", "Technology", 50000),
        ("Hamed", "Sales", 75000),
        ("Lina", "Technology", 65000),
        ("Omar", "HR", 40000)
    };

    var result =
        employees.AggregateBy(
            e => e.Department,
            dept => (Total: 0m, Count: 0),
            (acc, e) => (acc.Total + e.Salary, acc.Count + 1)
        );

    foreach (var item in result)
    {
        Console.WriteLine($"{item.Key}: Total={item.Value.Total}, Count={item.Value.Count}");
    }

    /*
     This code produces the following output:

     HR: Total=85000, Count=2
     Technology: Total=115000, Count=2
     Sales: Total=75000, Count=1
    */
}

Poznámky

Tato metoda je srovnatelná s GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) metodami, ve kterých je každé seskupení agregováno do jedné hodnoty, a nikoli přidělení kolekce pro každou skupinu.

Platí pro

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

Zdroj:
AggregateBy.cs
Zdroj:
AggregateBy.cs
Zdroj:
AggregateBy.cs

Použije funkci akumulátoru na sekvenci, seskupí výsledky podle klíče.

public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TAccumulate>> AggregateBy<TSource,TKey,TAccumulate>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func, System.Collections.Generic.IEqualityComparer<TKey>? keyComparer = default);
static member AggregateBy : seq<'Source> * Func<'Source, 'Key> * 'Accumulate * Func<'Accumulate, 'Source, 'Accumulate> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<System.Collections.Generic.KeyValuePair<'Key, 'Accumulate>>
<Extension()>
Public Function AggregateBy(Of TSource, TKey, TAccumulate) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), seed As TAccumulate, func As Func(Of TAccumulate, TSource, TAccumulate), Optional keyComparer As IEqualityComparer(Of TKey) = Nothing) As IEnumerable(Of KeyValuePair(Of TKey, TAccumulate))

Parametry typu

TSource

Typ prvků .source

TKey

Typ klíče vráceného řetězcem keySelector.

TAccumulate

Typ akumulátoru.

Parametry

source
IEnumerable<TSource>

Agregace IEnumerable<T> .

keySelector
Func<TSource,TKey>

Funkce, která extrahuje klíč pro každý prvek.

seed
TAccumulate

Počáteční hodnota akumulátoru.

func
Func<TAccumulate,TSource,TAccumulate>

Akumulátorová funkce, která má být vyvolána na každém prvku.

keyComparer
IEqualityComparer<TKey>

Porovnávat IEqualityComparer<T> klíče s.

Návraty

IEnumerable<KeyValuePair<TKey,TAccumulate>>

Výčet obsahující agregace odpovídající jednotlivým klíčům odvozeným od source.

Příklady

Následující příklad ukazuje použití AggregateBy s konstantní počáteční hodnotou pro výpočet součtů na klíč.

public static void AggregateBySeedExample()
{
    (string Name, string Department, decimal Salary)[] employees =
    {
        ("Ali", "HR", 45000),
        ("Samer", "Technology", 50000),
        ("Hamed", "Sales", 75000),
        ("Lina", "Technology", 65000),
        ("Omar", "HR", 40000)
    };

    var totals =
        employees.AggregateBy(
            e => e.Department,
            0m,
            (total, e) => total + e.Salary
        );

    foreach (var item in totals)
    {
        Console.WriteLine($"{item.Key}: {item.Value}");
    }

    /*
     This code produces the following output:

     HR: 85000
     Technology: 115000
     Sales: 75000
    */
}

Poznámky

Tato metoda je srovnatelná s GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) metodami, ve kterých se každé seskupení agreguje do jedné hodnoty, a nikoli přidělení kolekce pro každou skupinu.

Platí pro