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

Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
Aggregate.cs

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

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

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

例外

sourcefunc、または 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 が に resultSelector 渡され、 の Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)最終的な結果が取得されます。

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

適用対象

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

Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
Aggregate.cs

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

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 または funcnull です。

次のコード例では、 を使用 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返します。

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

適用対象

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

Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
Aggregate.cs

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

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 または funcnull です。

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>) 使用すると、一連の値に対して計算を簡単に実行できます。 このメソッドは、 の各要素に対して 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 メソッド、、 Countおよび 4 つの数値集計メソッド (つまり Min、、 MaxSumAverage) も含まれています。

適用対象