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

來源:
Aggregate.cs
來源:
Aggregate.cs
來源:
Aggregate.cs

將累加函式套用到序列上。 使用指定的值做為初始累加值,並使用指定的函式來選取結果值。

C#
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);

類型參數

TSource

source 項目的類型。

TAccumulate

累積值的類型。

TResult

結果值的類型。

參數

source
IEnumerable<TSource>

所要彙總 (Aggregate) 的 IEnumerable<T>

seed
TAccumulate

初始累積值。

func
Func<TAccumulate,TSource,TAccumulate>

要在每個項目上叫用 (Invoke) 的累加函式。

resultSelector
Func<TAccumulate,TResult>

用來將最終累加值轉換成結果值的函式。

傳回

TResult

轉換後的最終累加值。

例外狀況

sourcefuncresultSelectornull

範例

下列程式代碼範例示範如何使用 來套用 Aggregate 累積函數和結果選取器。

C#
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.

備註

方法 Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) 可讓您輕鬆地對值序列執行計算。 此方法的運作方式是針對 中的每個source項目呼叫func一次。 每次呼叫時,Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)都會func將序列中的 項目和匯總值傳遞 (做為第一個自變數來 func) 。 參數的值 seed 會當做初始匯總值使用。 的結果 func 會取代先前的匯總值。 的最終結果 func 會傳遞至 resultSelector ,以取得的最終結果 Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)

為了簡化常見的匯總作業,標準查詢運算子也包含一般用途的計數方法、 Count和四個數值匯總方法,也就是 MinMaxSumAverage

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

來源:
Aggregate.cs
來源:
Aggregate.cs
來源:
Aggregate.cs

將累加函式套用到序列上。 使用指定的初始值做為初始累加值。

C#
public static TAccumulate Aggregate<TSource,TAccumulate> (this System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func);

類型參數

TSource

source 項目的類型。

TAccumulate

累積值的類型。

參數

source
IEnumerable<TSource>

所要彙總 (Aggregate) 的 IEnumerable<T>

seed
TAccumulate

初始累積值。

func
Func<TAccumulate,TSource,TAccumulate>

要在每個項目上叫用 (Invoke) 的累加函式。

傳回

TAccumulate

最終累積值。

例外狀況

sourcefuncnull

範例

下列程式代碼範例示範如何使用 來套用 Aggregate 累積函數,並使用種子值。

C#
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

備註

方法 Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 可讓您輕鬆地對值序列執行計算。 此方法的運作方式是針對 中的每個source項目呼叫func一次。 每次呼叫時,Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)都會func將序列中的 項目和匯總值傳遞 (做為第一個自變數來 func) 。 參數的值 seed 會當做初始匯總值使用。 的結果 func 會取代先前的匯總值。 Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 傳回的最終結果 func

為了簡化常見的匯總作業,標準查詢運算子也包含一般用途的計數方法、 Count和四個數值匯總方法,也就是 MinMaxSumAverage

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

來源:
Aggregate.cs
來源:
Aggregate.cs
來源:
Aggregate.cs

將累加函式套用到序列上。

C#
public static TSource Aggregate<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TSource,TSource> func);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

所要彙總 (Aggregate) 的 IEnumerable<T>

func
Func<TSource,TSource,TSource>

要在每個項目上叫用 (Invoke) 的累加函式。

傳回

TSource

最終累積值。

例外狀況

sourcefuncnull

source 沒有包含任何項目。

範例

下列程式代碼範例示範如何使用 來反轉字串 Aggregate中的單字順序。

C#
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

備註

方法 Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) 可讓您輕鬆地對值序列執行計算。 此方法的運作方式是針對中每個元素呼叫 func 一次,但第一個元素 source 除外。 每次呼叫時,Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)都會func將序列中的 項目和匯總值傳遞 (做為第一個自變數來 func) 。 的第一個元素 source 會當做初始匯總值使用。 的結果 func 會取代先前的匯總值。 Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) 傳回的最終結果 func

此方法的 Aggregate 這個多載不適用於所有情況,因為它使用 的第一個專案 source 做為初始匯總值。 如果傳回值只包含符合特定條件的 source 元素,您應該選擇另一個多載。 例如,如果您想要計算 中 source偶數的總和,這個多載並不可靠。 如果第一個專案是奇數,而不是偶數,則結果會不正確。

為了簡化常見的匯總作業,標準查詢運算子也包含一般用途的計數方法、 Count和四個數值匯總方法,也就是 MinMaxSumAverage

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0