Enumerable.Aggregate メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
オーバーロード
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>)
シーケンスにアキュムレータ関数を適用します。 指定したシード値は最初のアキュムレータ値として使用され、指定した関数は結果値の選択に使用されます。
public:
generic <typename TSource, typename TAccumulate, typename TResult>
[System::Runtime::CompilerServices::Extension]
static TResult Aggregate(System::Collections::Generic::IEnumerable<TSource> ^ source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> ^ func, Func<TAccumulate, TResult> ^ resultSelector);
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);
static member Aggregate : seq<'Source> * 'Accumulate * Func<'Accumulate, 'Source, 'Accumulate> * Func<'Accumulate, 'Result> -> 'Result
<Extension()>
Public Function Aggregate(Of TSource, TAccumulate, TResult) (source As IEnumerable(Of TSource), seed As TAccumulate, func As Func(Of TAccumulate, TSource, TAccumulate), resultSelector As Func(Of TAccumulate, TResult)) As TResult
型パラメーター
- TSource
source
の要素の型。
- TAccumulate
アキュムレータ値の型。
- TResult
結果の値の型。
パラメーター
- source
- IEnumerable<TSource>
集計対象の IEnumerable<T>。
- seed
- TAccumulate
最初のアキュムレータ値。
- func
- Func<TAccumulate,TSource,TAccumulate>
各要素に対して呼び出すアキュムレータ関数。
- resultSelector
- Func<TAccumulate,TResult>
最終的なアキュムレータ値を結果値に変換する関数。
戻り値
- TResult
変換された最終的なアキュムレータ値。
例外
source
、func
、または resultSelector
は、null
です。
例
次のコード例では、アキュムレータ関数と結果セレクターを適用する方法 Aggregate を示します。
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.
Sub AggregateEx3()
Dim fruits() As String =
{"apple", "mango", "orange", "passionfruit", "grape"}
' Determine whether any string in the array is longer than "banana".
Dim longestName As String =
fruits.Aggregate("banana",
Function(ByVal longest, ByVal fruit) _
IIf(fruit.Length > longest.Length, fruit, longest),
Function(ByVal fruit) fruit.ToUpper())
' Display the output.
Console.WriteLine($"The fruit with the longest name is {longestName}")
End Sub
' 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
を取得Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)するためにresultSelector
渡されます。
一般的な集計操作を簡略化するために、標準のクエリ演算子には、汎用カウント メソッドと、Count4 つの数値集計メソッド (つまり、 MaxSumAverageMin
適用対象
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)
シーケンスにアキュムレータ関数を適用します。 指定されたシード値が最初のアキュムレータ値として使用されます。
public:
generic <typename TSource, typename TAccumulate>
[System::Runtime::CompilerServices::Extension]
static TAccumulate Aggregate(System::Collections::Generic::IEnumerable<TSource> ^ source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> ^ func);
public static TAccumulate Aggregate<TSource,TAccumulate> (this System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func);
static member Aggregate : seq<'Source> * 'Accumulate * Func<'Accumulate, 'Source, 'Accumulate> -> 'Accumulate
<Extension()>
Public Function Aggregate(Of TSource, TAccumulate) (source As IEnumerable(Of TSource), seed As TAccumulate, func As Func(Of TAccumulate, TSource, TAccumulate)) As TAccumulate
型パラメーター
- TSource
source
の要素の型。
- TAccumulate
アキュムレータ値の型。
パラメーター
- source
- IEnumerable<TSource>
集計対象の IEnumerable<T>。
- seed
- TAccumulate
最初のアキュムレータ値。
- func
- Func<TAccumulate,TSource,TAccumulate>
各要素に対して呼び出すアキュムレータ関数。
戻り値
- TAccumulate
最終的なアキュムレータ値。
例外
source
または func
が null
です。
例
次のコード例では、アキュムレータ関数を Aggregate 適用し、シード値を使用する方法を示します。
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
Sub AggregateEx2()
' Create an array of Integers.
Dim ints() As Integer = {4, 8, 8, 3, 9, 0, 7, 8, 2}
' Count the even numbers in the array, using a seed value of 0.
Dim numEven As Integer =
ints.Aggregate(0,
Function(ByVal total, ByVal number) _
IIf(number Mod 2 = 0, total + 1, total))
' Display the output.
Console.WriteLine($"The number of even integers is {numEven}")
End Sub
' 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
を返します。
一般的な集計操作を簡略化するために、標準のクエリ演算子には、汎用カウント メソッドと、Count4 つの数値集計メソッド (つまり、 MaxSumAverageMin
適用対象
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)
シーケンスにアキュムレータ関数を適用します。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Aggregate(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TSource, TSource> ^ func);
public static TSource Aggregate<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TSource,TSource> func);
static member Aggregate : seq<'Source> * Func<'Source, 'Source, 'Source> -> 'Source
<Extension()>
Public Function Aggregate(Of TSource) (source As IEnumerable(Of TSource), func As Func(Of TSource, TSource, TSource)) As TSource
型パラメーター
- TSource
source
の要素の型。
パラメーター
- source
- IEnumerable<TSource>
集計対象の IEnumerable<T>。
- func
- Func<TSource,TSource,TSource>
各要素に対して呼び出すアキュムレータ関数。
戻り値
- TSource
最終的なアキュムレータ値。
例外
source
または func
が null
です。
source
に要素が含まれていません。
例
次のコード例は、文字列 Aggregate内の単語の順序を逆にする方法を示しています。
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
Sub AggregateEx1()
Dim sentence As String =
"the quick brown fox jumps over the lazy dog"
' Split the string into individual words.
Dim words() As String = sentence.Split(" "c)
' Prepend each word to the beginning of the new sentence to reverse the word order.
Dim reversed As String =
words.Aggregate(Function(ByVal current, ByVal word) word & " " & current)
' Display the output.
Console.WriteLine(reversed)
End Sub
' This code produces the following output:
'
' dog lazy the over jumps fox brown quick the
注釈
この Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) メソッドを使用すると、一連の値に対して計算を簡単に実行できます。 このメソッドは、最初の要素を除く要素source
ごとに 1 回呼び出func
すことによって機能します。 呼 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
合計を計算する場合、このオーバーロードは信頼できません。 最初の要素が偶数ではなく奇数の場合、結果は正しくありません。
一般的な集計操作を簡略化するために、標準のクエリ演算子には、汎用カウント メソッドと、Count4 つの数値集計メソッド (つまり、 MaxSumAverageMin