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

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

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的类型。

TAccumulate

累加器值的类型。

TResult

结果值的类型。

参数

source
IEnumerable<TSource>

要聚合的集合 IEnumerable<T>

seed
TAccumulate

初始累加器值。

func
Func<TAccumulate,TSource,TAccumulate>

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

resultSelector
Func<TAccumulate,TResult>

一个函数,用于将最终累加器值转换为结果值。

返回

TResult

转换后的最终累加器值。

例外

sourcefuncresultSelectornull.

示例

下面的代码示例演示如何用于 Aggregate 应用累加器函数和结果选择器。

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.
    Console.WriteLine($"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>) 使对一系列值执行计算变得简单。 此方法的工作原理是针对每个func元素调用source一次。 每次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

适用于

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

Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
Aggregate.cs

对序列应用累加器函数。 指定的种子值用作初始累加器值。

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的类型。

TAccumulate

累加器值的类型。

参数

source
IEnumerable<TSource>

要聚合的集合 IEnumerable<T>

seed
TAccumulate

初始累加器值。

func
Func<TAccumulate,TSource,TAccumulate>

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

返回

TAccumulate

最终累加器值。

例外

sourcefuncnull.

示例

下面的代码示例演示如何使用 Aggregate 累加器函数并使用种子值。

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.
    Console.WriteLine($"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>) 使对一系列值执行计算变得简单。 此方法的工作原理是针对每个func元素调用source一次。 每次func调用时,Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)都会同时传递序列中的元素和聚合值(作为第一个参数)。func 参数的值 seed 用作初始聚合值。 替换上一个聚合值的结果 funcAggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 返回 . 的最终结果 func

为了简化常见的聚合操作,标准查询运算符还包括常规用途计数方法,Count以及四种数值聚合方法,即MinMaxSumAverage

适用于

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

Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
Aggregate.cs
Source:
Aggregate.cs

对序列应用累加器函数。

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的类型。

参数

source
IEnumerable<TSource>

要聚合的集合 IEnumerable<T>

func
Func<TSource,TSource,TSource>

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

返回

TSource

最终累加器值。

例外

sourcefuncnull.

source 不包含任何元素。

示例

下面的代码示例演示如何通过使用 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.
    Console.WriteLine(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>) 使对一系列值执行计算变得简单。 此方法的工作原理是为每个元素调用 func 一次,但第一个元素 source 除外。 每次func调用时,Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)都会同时传递序列中的元素和聚合值(作为第一个参数)。func 第一个元素 source 用作初始聚合值。 替换上一个聚合值的结果 funcAggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) 返回 . 的最终结果 func

此方法的 Aggregate 此重载不适用于所有情况,因为它使用第一个 source 元素作为初始聚合值。 如果返回值应仅包含满足特定条件的 source 元素,则应选择另一个重载。 例如,如果要计算偶 source数的总和,则此重载不可靠。 如果第一个元素是奇数而不是偶数,则结果将不正确。

为了简化常见的聚合操作,标准查询运算符还包括常规用途计数方法,Count以及四种数值聚合方法,即MinMaxSumAverage

适用于