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

轉換後的最終累加值。

例外狀況

sourcefuncselectornull

範例

下列程式代碼範例示範如何使用 來套用 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> 參數,其類型自變數為其中一個 Func<T,TResult> 型別。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 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) 參數的 屬性所Provider表示的 source 方法IQueryProvider

執行表示呼叫 Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的 source 實作。 預期的行為是,指定的函 func式會套用至來源序列中的每個值,並傳回累積值。 參數 seed 會做為累積值的種子值,其對應於 中的 func第一個參數。 最後累積的值會傳遞至 selector 以取得結果值。

為了簡化常見的匯總作業,標準查詢運算元集合也包含兩個計算方法,Count以及LongCount四個數值匯總方法,也就是 MaxMinSumAverage

適用於

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

最終累積值。

例外狀況

sourcefuncnull

範例

下列程式代碼範例示範如何在提供種子值給函式時,使用 來套用 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> 參數,其類型自變數為其中一個 Func<T,TResult> 型別。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 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) 參數的 屬性所Provider表示的 source 方法IQueryProvider

執行表示呼叫 Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的 source 實作。 預期的行為是,指定的函 func式會套用至來源序列中的每個值,並傳回累積值。 參數 seed 會做為累積值的種子值,其對應於 中的 func第一個參數。

為了簡化常見的匯總作業,標準查詢運算元集合也包含兩個計算方法,Count以及LongCount四個數值匯總方法,也就是 MaxMinSumAverage

適用於

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

最終累積值。

例外狀況

sourcefuncnull

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> 參數,其類型自變數為其中一個 Func<T,TResult> 型別。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 Expression<TDelegate>

方法 Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) 會產生 , MethodCallExpression 表示 Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) 呼叫本身做為建構的泛型方法。 然後,它會將 傳遞MethodCallExpressionExecute<TResult>(Expression) 參數的 屬性所Provider表示的 source 方法IQueryProvider

執行表示呼叫 Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的 source 實作。 預期的行為是,指定的函 func式會套用至來源序列中的每個值,並傳回累積值。 中的 source 第一個值會做為累積值的種子值,其對應於 中的 func第一個參數。

為了簡化常見的匯總作業,標準查詢運算元集合也包含兩個計算方法,Count以及LongCount四個數值匯總方法,也就是 MaxMinSumAverage

適用於