# Enumerable.AggregateEnumerable.AggregateEnumerable.AggregateEnumerable.Aggregate Method

## 多載

 Aggregate(IEnumerable, TAccumulate, Func, Func) Aggregate(IEnumerable, TAccumulate, Func, Func) Aggregate(IEnumerable, TAccumulate, Func, Func) Aggregate(IEnumerable, TAccumulate, Func, Func) 套用到序列的累加函式。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(IEnumerable, TAccumulate, Func) Aggregate(IEnumerable, TAccumulate, Func) Aggregate(IEnumerable, TAccumulate, Func) Aggregate(IEnumerable, TAccumulate, Func) 套用到序列的累加函式。Applies an accumulator function over a sequence. 使用指定的初始值做為初始累加值。The specified seed value is used as the initial accumulator value. Aggregate(IEnumerable, Func) Aggregate(IEnumerable, Func) Aggregate(IEnumerable, Func) Aggregate(IEnumerable, Func) 套用到序列的累加函式。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>)

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

TResult

#### 參數

source
IEnumerable<TSource>

IEnumerable<T>要彙總。An IEnumerable<T> to aggregate over.

seed
TAccumulate TAccumulate TAccumulate TAccumulate

func
Func<TAccumulate,TSource,TAccumulate>

resultSelector
Func<TAccumulate,TResult>

#### 傳回

TResult TResult TResult TResult

#### 例外狀況

`source``func``resultSelector``null``source` or `func` or `resultSelector` is `null`.

### 範例

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

### 備註

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`傳遞給`resultSelector`若要取得的最終結果Aggregate<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>).

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

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

#### 參數

source
IEnumerable<TSource>

IEnumerable<T>要彙總。An IEnumerable<T> to aggregate over.

seed
TAccumulate TAccumulate TAccumulate TAccumulate

func
Func<TAccumulate,TSource,TAccumulate>

#### 傳回

TAccumulate TAccumulate TAccumulate TAccumulate

#### 例外狀況

`source``func``null``source` or `func` is `null`.

### 範例

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

### 備註

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>) returns the final result of `func`.

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

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

#### 傳回

TSource TSource TSource TSource

#### 例外狀況

`source``func``null``source` or `func` is `null`.

`source` 包含任何項目。`source` contains no elements.

### 範例

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

### 備註

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>) 傳回的最終結果`func`Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) returns the final result of `func`.