Enumerable.Aggregate Enumerable.Aggregate Enumerable.Aggregate Enumerable.Aggregate Method

定義

オーバーロード

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

シーケンスにアキュムレータ関数を適用します。Applies an accumulator function over a sequence. 指定したシード値は最初のアキュムレータ値として使用され、指定した関数は結果値の選択に使用されます。The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

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

シーケンスにアキュムレータ関数を適用します。Applies an accumulator function over a sequence. 指定されたシード値が最初のアキュムレータ値として使用されます。The specified seed value is used as the initial accumulator value.

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

シーケンスにアキュムレータ関数を適用します。Applies an accumulator function over a sequence.

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

シーケンスにアキュムレータ関数を適用します。Applies an accumulator function over a sequence. 指定したシード値は最初のアキュムレータ値として使用され、指定した関数は結果値の選択に使用されます。The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

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 の要素の型。The type of the elements of source.

TAccumulate

アキュムレータ値の型。The type of the accumulator value.

TResult

結果の値の型。The type of the resulting value.

パラメーター

source
IEnumerable<TSource>

集計対象の IEnumerable<T>An IEnumerable<T> to aggregate over.

seed
TAccumulate TAccumulate TAccumulate TAccumulate

最初のアキュムレータ値。The initial accumulator value.

func
Func<TAccumulate,TSource,TAccumulate>

各要素に対して呼び出すアキュムレータ関数。An accumulator function to be invoked on each element.

resultSelector
Func<TAccumulate,TResult>

最終的なアキュムレータ値を結果値に変換する関数。A function to transform the final accumulator value into the result value.

戻り値

TResult TResult TResult TResult

変換された最終的なアキュムレータ値。The transformed final accumulator value.

例外

sourcefunc、または resultSelector は、null です。source or func or resultSelector is null.

次のコード例は、使用する方法を示しますAggregateアキュムレータ関数と結果のセレクターを適用します。The following code example demonstrates how to use Aggregate to apply an accumulator function and a result selector.

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.
    MsgBox("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>)メソッドでは、値のシーケンスに対して計算を実行する単純な。The Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) method makes it simple to perform a calculation over a sequence of values. このメソッドを呼び出すことで機能func内の各要素に 1 回sourceします。This method works by calling func one time for each element in source. 毎回funcが呼び出され、Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)シーケンスと、集計値から、両方の要素を渡します (最初の引数としてfunc)。Each time func is called, Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) passes both the element from the sequence and an aggregated value (as the first argument to func). 値、seedパラメーターは、最初の集計値として使用されます。The value of the seed parameter is used as the initial aggregate value. 結果func前の集計値を置き換えます。The result of func replaces the previous aggregated value. 最終結果funcに渡されるresultSelectorの最終的な結果を取得するAggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)します。The final result of func is passed to resultSelector to obtain the final result of Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>).

一般的な集計操作を簡素化する標準クエリ演算子も含める汎用 count メソッドでは、 Count、および 4 つの数値の集計メソッドでは、namely MinMaxSumAverageTo simplify common aggregation operations, the standard query operators also include a general purpose count method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.

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

シーケンスにアキュムレータ関数を適用します。Applies an accumulator function over a sequence. 指定されたシード値が最初のアキュムレータ値として使用されます。The specified seed value is used as the initial accumulator value.

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 の要素の型。The type of the elements of source.

TAccumulate

アキュムレータ値の型。The type of the accumulator value.

パラメーター

source
IEnumerable<TSource>

集計対象の IEnumerable<T>An IEnumerable<T> to aggregate over.

seed
TAccumulate TAccumulate TAccumulate TAccumulate

最初のアキュムレータ値。The initial accumulator value.

func
Func<TAccumulate,TSource,TAccumulate>

各要素に対して呼び出すアキュムレータ関数。An accumulator function to be invoked on each element.

戻り値

TAccumulate TAccumulate TAccumulate TAccumulate

最終的なアキュムレータ値。The final accumulator value.

例外

source または funcnull です。source or func is null.

次のコード例は、使用する方法を示しますAggregateアキュムレータ関数を適用して、シード値を使用します。The following code example demonstrates how to use Aggregate to apply an accumulator function and use a seed value.

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.
    MsgBox("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>)メソッドでは、値のシーケンスに対して計算を実行する単純な。The Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) method makes it simple to perform a calculation over a sequence of values. このメソッドを呼び出すことで機能func内の各要素に 1 回sourceします。This method works by calling func one time for each element in source. 毎回funcが呼び出され、Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)シーケンスと、集計値から、両方の要素を渡します (最初の引数としてfunc)。Each time func is called, Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) passes both the element from the sequence and an aggregated value (as the first argument to func). 値、seedパラメーターは、最初の集計値として使用されます。The value of the seed parameter is used as the initial aggregate value. 結果func前の集計値を置き換えます。The result of func replaces the previous aggregated value. Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 最終的な結果を返しますfuncします。Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) returns the final result of func.

一般的な集計操作を簡素化する標準クエリ演算子も含める汎用 count メソッドでは、 Count、および 4 つの数値の集計メソッドでは、namely MinMaxSumAverageTo simplify common aggregation operations, the standard query operators also include a general purpose count method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.

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

シーケンスにアキュムレータ関数を適用します。Applies an accumulator function over a sequence.

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 の要素の型。The type of the elements of source.

パラメーター

source
IEnumerable<TSource>

集計対象の IEnumerable<T>An IEnumerable<T> to aggregate over.

func
Func<TSource,TSource,TSource>

各要素に対して呼び出すアキュムレータ関数。An accumulator function to be invoked on each element.

戻り値

TSource TSource TSource TSource

最終的なアキュムレータ値。The final accumulator value.

例外

source または funcnull です。source or func is null.

source に要素が含まれていません。source contains no elements.

次のコード例を使用して、文字列内の単語の順序を反転する方法を示しますAggregateします。The following code example demonstrates how to reverse the order of words in a string by using 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.
    MsgBox(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>)メソッドでは、値のシーケンスに対して計算を実行する単純な。The Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) method makes it simple to perform a calculation over a sequence of values. このメソッドを呼び出すことで機能func内の各要素に 1 回source1 つ目を除きます。This method works by calling func one time for each element in source except the first one. 毎回funcが呼び出され、Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)シーケンスと、集計値から、両方の要素を渡します (最初の引数としてfunc)。Each time func is called, Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) passes both the element from the sequence and an aggregated value (as the first argument to func). 最初の要素のsource最初の集計値として使用されます。The first element of source is used as the initial aggregate value. 結果func前の集計値を置き換えます。The result of func replaces the previous aggregated value. Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) 最終的な結果を返しますfuncします。Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) returns the final result of func.

このオーバー ロード、Aggregateの最初の要素を使用しているために、メソッドはすべてのケースに適したsource最初の集計値として。This overload of the Aggregate method isn't suitable for all cases because it uses the first element of source as the initial aggregate value. 戻り値の要素のみを含める必要がある場合は、別のオーバー ロードを選択する必要がありますsource特定の条件を満たします。You should choose another overload if the return value should include only the elements of source that meet a certain condition. たとえば、このオーバー ロードは偶数の数値の合計を計算する場合は、信頼性の高いsourceします。For example, this overload isn't reliable if you want to calculate the sum of the even numbers in source. 結果は最初の要素があってものではなく奇数の場合は、正しいできません。The result will be incorrect if the first element is odd instead of even.

一般的な集計操作を簡素化する標準クエリ演算子も含める汎用 count メソッドでは、 Count、および 4 つの数値の集計メソッドでは、namely MinMaxSumAverageTo simplify common aggregation operations, the standard query operators also include a general purpose count method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.

適用対象