次の方法で共有


Enumerable.AggregateBy メソッド

定義

オーバーロード

名前 説明
AggregateBy<TSource,TKey,TAccumulate>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TKey,TAccumulate>, Func<TAccumulate,TSource,TAccumulate>, IEqualityComparer<TKey>)

アキュムレータ関数をシーケンスに適用し、結果をキーでグループ化します。

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

アキュムレータ関数をシーケンスに適用し、結果をキーでグループ化します。

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

ソース:
AggregateBy.cs
ソース:
AggregateBy.cs
ソース:
AggregateBy.cs

アキュムレータ関数をシーケンスに適用し、結果をキーでグループ化します。

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

型パラメーター

TSource

sourceの要素の型。

TKey

keySelectorによって返されるキーの型。

TAccumulate

アキュムレータ値の型。

パラメーター

source
IEnumerable<TSource>

集計する IEnumerable<T>

keySelector
Func<TSource,TKey>

各要素のキーを抽出する関数。

seedSelector
Func<TKey,TAccumulate>

初期アキュムレータ値のファクトリ。

func
Func<TAccumulate,TSource,TAccumulate>

各要素で呼び出されるアキュムレータ関数。

keyComparer
IEqualityComparer<TKey>

キーを比較する IEqualityComparer<T>

返品

IEnumerable<KeyValuePair<TKey,TAccumulate>>

sourceから派生する各キーに対応する集計を含む列挙可能。

次の例では、シード セレクターで AggregateBy を使用して、キーごとに複数の値を計算する方法を示します。

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

注釈

このメソッドは、各グループにコレクションを割り当てるのではなく、各グループが 1 つの値に集計される GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) メソッドと同等です。

適用対象

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

ソース:
AggregateBy.cs
ソース:
AggregateBy.cs
ソース:
AggregateBy.cs

アキュムレータ関数をシーケンスに適用し、結果をキーでグループ化します。

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

型パラメーター

TSource

sourceの要素の型。

TKey

keySelectorによって返されるキーの型。

TAccumulate

アキュムレータ値の型。

パラメーター

source
IEnumerable<TSource>

集計する IEnumerable<T>

keySelector
Func<TSource,TKey>

各要素のキーを抽出する関数。

seed
TAccumulate

初期アキュムレータ値。

func
Func<TAccumulate,TSource,TAccumulate>

各要素で呼び出されるアキュムレータ関数。

keyComparer
IEqualityComparer<TKey>

キーを比較する IEqualityComparer<T>

返品

IEnumerable<KeyValuePair<TKey,TAccumulate>>

sourceから派生する各キーに対応する集計を含む列挙可能。

次の例では、定数シード値を持つ AggregateBy を使用して、キーごとの合計を計算する方法を示します。

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

注釈

このメソッドは、各グループにコレクションを割り当てるのではなく、各グループが 1 つの値に集計される GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) メソッドと同等です。

適用対象