Bagikan melalui


Enumerable.AggregateBy Metode

Definisi

Overload

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

Sumber:
AggregateBy.cs
Sumber:
AggregateBy.cs
Sumber:
AggregateBy.cs

Menerapkan fungsi akumulator secara berurutan, mengelompokkan hasil menurut kunci.

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))

Jenis parameter

TSource

Jenis elemen source.

TKey

Jenis kunci yang dikembalikan oleh keySelector.

TAccumulate

Jenis nilai akumulator.

Parameter

source
IEnumerable<TSource>

Untuk IEnumerable<T> mengagregasi.

keySelector
Func<TSource,TKey>

Fungsi untuk mengekstrak kunci untuk setiap elemen.

seedSelector
Func<TKey,TAccumulate>

Pabrik untuk nilai akumulator awal.

func
Func<TAccumulate,TSource,TAccumulate>

Fungsi akumulator yang akan dipanggil pada setiap elemen.

keyComparer
IEqualityComparer<TKey>

Untuk IEqualityComparer<T> membandingkan kunci dengan.

Mengembalikan

IEnumerable<KeyValuePair<TKey,TAccumulate>>

Enumerable yang berisi agregat yang sesuai dengan setiap kunci yang berasal dari source.

Contoh

Contoh berikut menunjukkan cara menggunakan AggregateBy dengan pemilih benih untuk menghitung beberapa nilai per kunci.

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
    */
}

Keterangan

Metode ini sebanding dengan GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) metode di mana setiap pengelompokan dikumpulkan ke dalam satu nilai dibandingkan dengan mengalokasikan koleksi untuk setiap grup.

Berlaku untuk

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

Sumber:
AggregateBy.cs
Sumber:
AggregateBy.cs
Sumber:
AggregateBy.cs

Menerapkan fungsi akumulator secara berurutan, mengelompokkan hasil menurut kunci.

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))

Jenis parameter

TSource

Jenis elemen source.

TKey

Jenis kunci yang dikembalikan oleh keySelector.

TAccumulate

Jenis nilai akumulator.

Parameter

source
IEnumerable<TSource>

Untuk IEnumerable<T> mengagregasi.

keySelector
Func<TSource,TKey>

Fungsi untuk mengekstrak kunci untuk setiap elemen.

seed
TAccumulate

Nilai akumulator awal.

func
Func<TAccumulate,TSource,TAccumulate>

Fungsi akumulator yang akan dipanggil pada setiap elemen.

keyComparer
IEqualityComparer<TKey>

Untuk IEqualityComparer<T> membandingkan kunci dengan.

Mengembalikan

IEnumerable<KeyValuePair<TKey,TAccumulate>>

Enumerable yang berisi agregat yang sesuai dengan setiap kunci yang berasal dari source.

Contoh

Contoh berikut menunjukkan cara menggunakan AggregateBy dengan nilai benih konstan untuk menghitung total per kunci.

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
    */
}

Keterangan

Metode ini sebanding dengan GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) metode di mana setiap pengelompokan dikumpulkan ke dalam satu nilai dibandingkan dengan mengalokasikan koleksi untuk setiap grup.

Berlaku untuk