Enumerable.Aggregate Enumerable.Aggregate Enumerable.Aggregate Enumerable.Aggregate Method

定义

重载

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

对序列应用累加器函数。Applies an accumulator function over a sequence. 将指定的种子值用作累加器的初始值,并使用指定的函数选择结果值。The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

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

对序列应用累加器函数。Applies an accumulator function over a sequence. 将指定的种子值用作累加器初始值。The specified seed value is used as the initial accumulator value.

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

对序列应用累加器函数。Applies an accumulator function over a sequence.

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

对序列应用累加器函数。Applies an accumulator function over a sequence. 将指定的种子值用作累加器的初始值,并使用指定的函数选择结果值。The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

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 的元素类型。The type of the elements of source.

TAccumulate

累加器值的类型。The type of the accumulator value.

TResult

结果值的类型。The type of the resulting value.

参数

source
IEnumerable<TSource>

要聚合的 IEnumerable<T>An IEnumerable<T> to aggregate over.

seed
TAccumulate TAccumulate TAccumulate TAccumulate

累加器的初始值。The initial accumulator value.

func
Func<TAccumulate,TSource,TAccumulate>

要对每个元素调用的累加器函数。An accumulator function to be invoked on each element.

resultSelector
Func<TAccumulate,TResult>

将累加器的最终值转换为结果值的函数。A function to transform the final accumulator value into the result value.

返回

TResult TResult TResult TResult

已转换的累加器最终值。The transformed final accumulator value.

异常

sourcefuncresultSelectornullsource or func or resultSelector is null.

示例

下面的代码示例演示如何使用Aggregate来应用累加器函数和结果选择器。The following code example demonstrates how to use Aggregate to apply an accumulator function and a result selector.

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.
    MsgBox("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>)方法可以简单地对值序列执行计算。The Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) method makes it simple to perform a calculation over a sequence of values. 此方法的工作方式func是对中source的每个元素调用一次。This method works by calling func one time for each element in source. 每次func调用时, Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)都会同时传递序列中的元素和聚合值 ( func作为的第一个参数)。Each time func is called, Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) passes both the element from the sequence and an aggregated value (as the first argument to func). seed参数的值用作初始聚合值。The value of the seed parameter is used as the initial aggregate value. 替换上一个func聚合值的结果。The result of func replaces the previous aggregated value. 的最终结果func传递到resultSelectorAggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)获取的最终结果。The final result of func is passed to resultSelector to obtain the final result of Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>).

为了简化常见聚合运算, 标准查询运算符还包括常规用途Count计数方法、和四个数字聚合方法, MaxMin Sum、、和AverageTo simplify common aggregation operations, the standard query operators also include a general purpose count method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.

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

对序列应用累加器函数。Applies an accumulator function over a sequence. 将指定的种子值用作累加器初始值。The specified seed value is used as the initial accumulator value.

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 的元素类型。The type of the elements of source.

TAccumulate

累加器值的类型。The type of the accumulator value.

参数

source
IEnumerable<TSource>

要聚合的 IEnumerable<T>An IEnumerable<T> to aggregate over.

seed
TAccumulate TAccumulate TAccumulate TAccumulate

累加器的初始值。The initial accumulator value.

func
Func<TAccumulate,TSource,TAccumulate>

要对每个元素调用的累加器函数。An accumulator function to be invoked on each element.

返回

TAccumulate TAccumulate TAccumulate TAccumulate

累加器的最终值。The final accumulator value.

异常

sourcefuncnullsource or func is null.

示例

下面的代码示例演示如何使用Aggregate来应用累加器函数并使用种子值。The following code example demonstrates how to use Aggregate to apply an accumulator function and use a seed value.

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.
    MsgBox("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>)方法可以简单地对值序列执行计算。The Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) method makes it simple to perform a calculation over a sequence of values. 此方法的工作方式func是对中source的每个元素调用一次。This method works by calling func one time for each element in source. 每次func调用时, Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)都会同时传递序列中的元素和聚合值 ( func作为的第一个参数)。Each time func is called, Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) passes both the element from the sequence and an aggregated value (as the first argument to func). seed参数的值用作初始聚合值。The value of the seed parameter is used as the initial aggregate value. 替换上一个func聚合值的结果。The result of func replaces the previous aggregated value. Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)返回最终结果funcAggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) returns the final result of func.

为了简化常见聚合运算, 标准查询运算符还包括常规用途Count计数方法、和四个数字聚合方法, MaxMin Sum、、和AverageTo simplify common aggregation operations, the standard query operators also include a general purpose count method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.

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

对序列应用累加器函数。Applies an accumulator function over a sequence.

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 的元素类型。The type of the elements of source.

参数

source
IEnumerable<TSource>

要聚合的 IEnumerable<T>An IEnumerable<T> to aggregate over.

func
Func<TSource,TSource,TSource>

要对每个元素调用的累加器函数。An accumulator function to be invoked on each element.

返回

TSource TSource TSource TSource

累加器的最终值。The final accumulator value.

异常

sourcefuncnullsource or func is null.

source 中不包含任何元素。source contains no elements.

示例

下面的代码示例演示如何通过使用Aggregate反转字符串中单词的顺序。The following code example demonstrates how to reverse the order of words in a string by using 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.
    MsgBox(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>)方法可以简单地对值序列执行计算。The Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) method makes it simple to perform a calculation over a sequence of values. 此方法的工作方式func是对中source的每个元素调用一次, 但第一个元素除外。This method works by calling func one time for each element in source except the first one. 每次func调用时, Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)都会同时传递序列中的元素和聚合值 ( func作为的第一个参数)。Each time func is called, Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) passes both the element from the sequence and an aggregated value (as the first argument to func). 的第一个元素source用作初始聚合值。The first element of source is used as the initial aggregate value. 替换上一个func聚合值的结果。The result of func replaces the previous aggregated value. Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)返回最终结果funcAggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) returns the final result of func.

Aggregate方法的重载不适用于所有情况, 因为它使用的第一个source元素作为初始聚合值。This overload of the Aggregate method isn't suitable for all cases because it uses the first element of source as the initial aggregate value. 如果返回值仅应包含符合特定条件的元素source , 则应选择另一个重载。You should choose another overload if the return value should include only the elements of source that meet a certain condition. 例如, 如果要计算中source偶数的总和, 此重载并不可靠。For example, this overload isn't reliable if you want to calculate the sum of the even numbers in source. 如果第一个元素是奇数而不是偶数, 则结果将不正确。The result will be incorrect if the first element is odd instead of even.

为了简化常见聚合运算, 标准查询运算符还包括常规用途Count计数方法、和四个数字聚合方法, MaxMin Sum、、和AverageTo simplify common aggregation operations, the standard query operators also include a general purpose count method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.

适用于