Queryable.Aggregate メソッド

定義

オーバーロード

Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>)

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

Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>)

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

Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>)

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

Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

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

public:
generic <typename TSource, typename TAccumulate, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static TResult Aggregate(System::Linq::IQueryable<TSource> ^ source, TAccumulate seed, System::Linq::Expressions::Expression<Func<TAccumulate, TSource, TAccumulate> ^> ^ func, System::Linq::Expressions::Expression<Func<TAccumulate, TResult> ^> ^ selector);
public static TResult Aggregate<TSource,TAccumulate,TResult> (this System.Linq.IQueryable<TSource> source, TAccumulate seed, System.Linq.Expressions.Expression<Func<TAccumulate,TSource,TAccumulate>> func, System.Linq.Expressions.Expression<Func<TAccumulate,TResult>> selector);
static member Aggregate : System.Linq.IQueryable<'Source> * 'Accumulate * System.Linq.Expressions.Expression<Func<'Accumulate, 'Source, 'Accumulate>> * System.Linq.Expressions.Expression<Func<'Accumulate, 'Result>> -> 'Result
<Extension()>
Public Function Aggregate(Of TSource, TAccumulate, TResult) (source As IQueryable(Of TSource), seed As TAccumulate, func As Expression(Of Func(Of TAccumulate, TSource, TAccumulate)), selector As Expression(Of Func(Of TAccumulate, TResult))) As TResult

型パラメーター

TSource

source の要素の型。

TAccumulate

アキュムレータ値の型。

TResult

結果の値の型。

パラメーター

source
IQueryable<TSource>

集計対象のシーケンス。

seed
TAccumulate

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

func
Expression<Func<TAccumulate,TSource,TAccumulate>>

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

selector
Expression<Func<TAccumulate,TResult>>

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

戻り値

TResult

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

例外

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

次のコード例では、 を使用 Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) してアキュムレータ関数と結果セレクターを適用する方法を示します。

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

// Determine whether any string in the array is longer than "banana".
string longestName =
    fruits.AsQueryable().Aggregate(
    "banana",
    (longest, next) => next.Length > longest.Length ? next : longest,
    // Return the final result as an uppercase 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.
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.AsQueryable().Aggregate( _
    "banana", _
    Function(ByVal longest, ByVal fruit) IIf(fruit.Length > longest.Length, fruit, longest), _
    Function(ByVal fruit) fruit.ToUpper() _
)

MsgBox(String.Format( _
    "The fruit with the longest name is {0}.", longestName) _
)

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

注釈

このメソッドには、型引数が型 Expression<TDelegate> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターでは、ラムダ式を渡すことができます。これは に Expression<TDelegate>コンパイルされます。

メソッドは Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>)MethodCallExpression 構築されたジェネリック メソッドとして自身を呼び出すことを Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) 表す を生成します。 次に、 パラメーターの MethodCallExpressionExecute<TResult>(Expression) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。

呼び出し Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 func指定された関数 である がソース シーケンス内の各値に適用され、累積値が返されるということです。 パラメーターは seed 、累積値のシード値として使用されます。これは、 の最初のパラメーターに func対応します。 最終的な累積値が に selector 渡され、結果の値が取得されます。

一般的な集計操作を簡略化するために、標準クエリ演算子のセットには、2 つのカウント メソッドとLongCountCount と の 4 つの数値集計メソッド (、、Sum、およびAverage) MaxMinも含まれています。

適用対象

Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

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

public:
generic <typename TSource, typename TAccumulate>
[System::Runtime::CompilerServices::Extension]
 static TAccumulate Aggregate(System::Linq::IQueryable<TSource> ^ source, TAccumulate seed, System::Linq::Expressions::Expression<Func<TAccumulate, TSource, TAccumulate> ^> ^ func);
public static TAccumulate Aggregate<TSource,TAccumulate> (this System.Linq.IQueryable<TSource> source, TAccumulate seed, System.Linq.Expressions.Expression<Func<TAccumulate,TSource,TAccumulate>> func);
static member Aggregate : System.Linq.IQueryable<'Source> * 'Accumulate * System.Linq.Expressions.Expression<Func<'Accumulate, 'Source, 'Accumulate>> -> 'Accumulate
<Extension()>
Public Function Aggregate(Of TSource, TAccumulate) (source As IQueryable(Of TSource), seed As TAccumulate, func As Expression(Of Func(Of TAccumulate, TSource, TAccumulate))) As TAccumulate

型パラメーター

TSource

source の要素の型。

TAccumulate

アキュムレータ値の型。

パラメーター

source
IQueryable<TSource>

集計対象のシーケンス。

seed
TAccumulate

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

func
Expression<Func<TAccumulate,TSource,TAccumulate>>

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

戻り値

TAccumulate

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

例外

source または funcnull です。

次のコード例は、 を使用 Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) して、シード値が関数に提供されるときにアキュムレータ関数を適用する方法を示しています。

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.AsQueryable().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
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.AsQueryable().Aggregate( _
        0, _
        Function(ByVal total, ByVal number) _
            IIf(number Mod 2 = 0, total + 1, total) _
    )

MsgBox(String.Format("The number of even integers is: {0}", numEven))

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

注釈

このメソッドには、型引数が型 Expression<TDelegate> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターでは、ラムダ式を渡すことができます。これは に Expression<TDelegate>コンパイルされます。

メソッドは Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>)MethodCallExpression 構築されたジェネリック メソッドとして自身を呼び出すことを Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) 表す を生成します。 次に、 パラメーターの MethodCallExpressionExecute<TResult>(Expression) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。

呼び出し Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 func指定された関数 である がソース シーケンス内の各値に適用され、累積値が返されるということです。 パラメーターは seed 、累積値のシード値として使用されます。これは、 の最初のパラメーターに func対応します。

一般的な集計操作を簡略化するために、標準クエリ演算子のセットには、2 つのカウント メソッドとLongCountCount と の 4 つの数値集計メソッド (、、Sum、およびAverage) MaxMinも含まれています。

適用対象

Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource Aggregate(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TSource, TSource> ^> ^ func);
public static TSource Aggregate<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TSource,TSource>> func);
static member Aggregate : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Source, 'Source>> -> 'Source
<Extension()>
Public Function Aggregate(Of TSource) (source As IQueryable(Of TSource), func As Expression(Of Func(Of TSource, TSource, TSource))) As TSource

型パラメーター

TSource

source の要素の型。

パラメーター

source
IQueryable<TSource>

集計対象のシーケンス。

func
Expression<Func<TSource,TSource,TSource>>

各要素に適用するアキュムレータ関数。

戻り値

TSource

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

例外

source または funcnull です。

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

次のコード例では、 を使用 Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) して文字列の配列から文を作成する方法を示します。

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

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

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

Console.WriteLine(reversed);

// This code produces the following output:
//
// dog lazy the over jumps fox brown quick the
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)

' Use Aggregate() to prepend each word to the beginning of the 
' new sentence to reverse the word order.
Dim reversed As String = _
    words.AsQueryable().Aggregate( _
        Function(ByVal workingSentence, ByVal nextWord) nextWord & " " & workingSentence _
    )

MsgBox(reversed)

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

注釈

このメソッドには、型引数が型 Expression<TDelegate> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターでは、ラムダ式を渡すことができます。これは に Expression<TDelegate>コンパイルされます。

メソッドは Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>)MethodCallExpression 構築されたジェネリック メソッドとして自身を呼び出すことを Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) 表す を生成します。 次に、 パラメーターの MethodCallExpressionExecute<TResult>(Expression) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。

呼び出し Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 func指定された関数 である がソース シーケンス内の各値に適用され、累積値が返されるということです。 の最初の source 値は、累積値のシード値として使用されます。これは、 の最初のパラメーターに func対応します。

一般的な集計操作を簡略化するために、標準クエリ演算子のセットには、2 つのカウント メソッドとLongCountCount と の 4 つの数値集計メソッド (、、Sum、およびAverage) MaxMinも含まれています。

適用対象