英語で読む

次の方法で共有


Enumerable.Aggregate メソッド

定義

オーバーロード

Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)

シーケンスにアキュムレータ関数を適用します。 指定したシード値は最初のアキュムレータ値として使用され、指定した関数は結果値の選択に使用されます。

Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)

シーケンスにアキュムレータ関数を適用します。 指定されたシード値が最初のアキュムレータ値として使用されます。

Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)

シーケンスにアキュムレータ関数を適用します。

Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)

ソース:
Aggregate.cs
ソース:
Aggregate.cs
ソース:
Aggregate.cs

シーケンスにアキュムレータ関数を適用します。 指定したシード値は最初のアキュムレータ値として使用され、指定した関数は結果値の選択に使用されます。

C#
public static TResult Aggregate<TSource,TAccumulate,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func, Func<TAccumulate,TResult> resultSelector);

型パラメーター

TSource

source の要素の型。

TAccumulate

アキュムレータ値の型。

TResult

結果の値の型。

パラメーター

source
IEnumerable<TSource>

集計対象の IEnumerable<T>

seed
TAccumulate

最初のアキュムレータ値。

func
Func<TAccumulate,TSource,TAccumulate>

各要素に対して呼び出すアキュムレータ関数。

resultSelector
Func<TAccumulate,TResult>

最終的なアキュムレータ値を結果値に変換する関数。

戻り値

TResult

変換された最終的なアキュムレータ値。

例外

sourcefunc、または resultSelector は、null です。

次のコード例では、 を使用 Aggregate してアキュムレータ関数と結果セレクターを適用する方法を示します。

C#
string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" };

// Determine whether any string in the array is longer than "banana".
string longestName =
    fruits.Aggregate("banana",
                    (longest, next) =>
                        next.Length > longest.Length ? next : longest,
    // Return the final result as an upper case string.
                    fruit => fruit.ToUpper());

Console.WriteLine(
    "The fruit with the longest name is {0}.",
    longestName);

// This code produces the following output:
//
// The fruit with the longest name is PASSIONFRUIT.

注釈

メソッドを Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) 使用すると、一連の値に対する計算を簡単に実行できます。 このメソッドは、 内sourceの要素ごとに 1 回呼び出funcすことによって機能します。 funcが呼び出されるたびに、Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)シーケンスの 要素と集計値の両方を (最初の引数として) にfunc渡します。 パラメーターの seed 値は、初期集計値として使用されます。 の func 結果は、前の集計値を置き換えます。 の最終的な結果 func が に resultSelector 渡され、 の Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)最終的な結果が取得されます。

一般的な集計操作を簡略化するために、標準クエリ演算子には、汎用カウント メソッド 、、Countおよび 4 つの数値集計メソッド (、 などMinMaxSumAverage) も含まれます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)

ソース:
Aggregate.cs
ソース:
Aggregate.cs
ソース:
Aggregate.cs

シーケンスにアキュムレータ関数を適用します。 指定されたシード値が最初のアキュムレータ値として使用されます。

C#
public static TAccumulate Aggregate<TSource,TAccumulate> (this System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func);

型パラメーター

TSource

source の要素の型。

TAccumulate

アキュムレータ値の型。

パラメーター

source
IEnumerable<TSource>

集計対象の IEnumerable<T>

seed
TAccumulate

最初のアキュムレータ値。

func
Func<TAccumulate,TSource,TAccumulate>

各要素に対して呼び出すアキュムレータ関数。

戻り値

TAccumulate

最終的なアキュムレータ値。

例外

source または funcnull です。

次のコード例では、 を使用 Aggregate してアキュムレータ関数を適用し、シード値を使用する方法を示します。

C#
int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };

// Count the even numbers in the array, using a seed value of 0.
int numEven = ints.Aggregate(0, (total, next) =>
                                    next % 2 == 0 ? total + 1 : total);

Console.WriteLine("The number of even integers is: {0}", numEven);

// This code produces the following output:
//
// The number of even integers is: 6

注釈

メソッドを Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 使用すると、一連の値に対する計算を簡単に実行できます。 このメソッドは、 内sourceの要素ごとに 1 回呼び出funcすことによって機能します。 funcが呼び出されるたびに、Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)シーケンスの 要素と集計値の両方を (最初の引数として) にfunc渡します。 パラメーターの seed 値は、初期集計値として使用されます。 の func 結果は、前の集計値を置き換えます。 Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) は の最終的な結果を func返します。

一般的な集計操作を簡略化するために、標準クエリ演算子には、汎用カウント メソッド 、、Countおよび 4 つの数値集計メソッド (、 などMinMaxSumAverage) も含まれます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)

ソース:
Aggregate.cs
ソース:
Aggregate.cs
ソース:
Aggregate.cs

シーケンスにアキュムレータ関数を適用します。

C#
public static TSource Aggregate<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TSource,TSource> func);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

集計対象の IEnumerable<T>

func
Func<TSource,TSource,TSource>

各要素に対して呼び出すアキュムレータ関数。

戻り値

TSource

最終的なアキュムレータ値。

例外

source または funcnull です。

source に要素が含まれていません。

次のコード例では、 を使用 Aggregateして文字列内の単語の順序を逆にする方法を示します。

C#
string sentence = "the quick brown fox jumps over the lazy dog";

// Split the string into individual words.
string[] words = sentence.Split(' ');

// Prepend each word to the beginning of the
// new sentence to reverse the word order.
string reversed = words.Aggregate((workingSentence, next) =>
                                      next + " " + workingSentence);

Console.WriteLine(reversed);

// This code produces the following output:
//
// dog lazy the over jumps fox brown quick the

注釈

メソッドを Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) 使用すると、一連の値に対する計算を簡単に実行できます。 このメソッドは、 の各要素に対して 1 回呼び出 func すことによって機能します。ただし、最初の要素 source は除きます。 funcが呼び出されるたびに、Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)シーケンスの 要素と集計値の両方を (最初の引数として) にfunc渡します。 の最初の source 要素は、初期集計値として使用されます。 の func 結果は、前の集計値を置き換えます。 Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) は の最終的な結果を func返します。

メソッドの Aggregate このオーバーロードは、 の最初の要素 source を初期集計値として使用するため、すべてのケースに適していません。 戻り値に特定の条件を満たす の要素のみを含める必要がある場合は、別の source オーバーロードを選択する必要があります。 たとえば、 の偶数の source合計を計算する場合、このオーバーロードは信頼できません。 最初の要素が偶数ではなく奇数の場合、結果は正しくありません。

一般的な集計操作を簡略化するために、標準クエリ演算子には、汎用カウント メソッド 、、Countおよび 4 つの数値集計メソッド (、 などMinMaxSumAverage) も含まれます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0