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

对序列应用累加器函数。 将指定的种子值用作累加器的初始值,并使用指定的函数选择结果值。

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>

要聚合的 IEnumerable<T>

seed
TAccumulate

累加器的初始值。

func
Func<TAccumulate,TSource,TAccumulate>

要对每个元素调用的累加器函数。

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一次。 每次 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四个数值聚合方法,即 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>)

Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
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>

要聚合的 IEnumerable<T>

seed
TAccumulate

累加器的初始值。

func
Func<TAccumulate,TSource,TAccumulate>

要对每个元素调用的累加器函数。

返回

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一次。 每次 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四个数值聚合方法,即 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>)

Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
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>

要聚合的 IEnumerable<T>

func
Func<TSource,TSource,TSource>

要对每个元素调用的累加器函数。

返回

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>) 使对值序列执行计算变得简单。 此方法的工作原理是,除第一个元素外,对 中的每个source元素调用func一次。 每次 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四个数值聚合方法,即 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