Enumerable.Aggregate Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
|
Aggregate |
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 |
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value. |
|
Aggregate |
Applies an accumulator function over a sequence. |
Aggregate(IEnumerable, TAccumulate, Func, Func)
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
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
[System::Runtime::CompilerServices::Extension]
static TResult Aggregate(System::Collections::Generic::IEnumerable ^ source, TAccumulate seed, Func ^ func, Func ^ resultSelector);
public static TResult Aggregate(this System.Collections.Generic.IEnumerable source, TAccumulate seed, Func func, Func resultSelector);
static member Aggregate : seq<'Source> * 'Accumulate * Func<'Accumulate, 'Source, 'Accumulate> * Func<'Accumulate, 'Result> -> 'Result
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
Type Parameters
- TSource
The type of the elements of source.
- TAccumulate
The type of the accumulator value.
- TResult
The type of the resulting value.
Parameters
- source
-
IEnumerable
An IEnumerable
- seed
- TAccumulate
The initial accumulator value.
- func
-
Func
An accumulator function to be invoked on each element.
- resultSelector
-
Func
A function to transform the final accumulator value into the result value.
Returns
The transformed final accumulator value.
Exceptions
source or func or resultSelector is null.
Examples
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.
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
Remarks
The Aggregatefunc one time for each element in source. Each time func is called, Aggregatefunc). The value of the seed parameter is used as the initial aggregate value. The result of func replaces the previous aggregated value. The final result of func is passed to resultSelector to obtain the final result of Aggregate
To 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.
Applies to
Aggregate(IEnumerable, TAccumulate, Func)
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
public:
generic
[System::Runtime::CompilerServices::Extension]
static TAccumulate Aggregate(System::Collections::Generic::IEnumerable ^ source, TAccumulate seed, Func ^ func);
public static TAccumulate Aggregate(this System.Collections.Generic.IEnumerable source, TAccumulate seed, Func func);
static member Aggregate : seq<'Source> * 'Accumulate * Func<'Accumulate, 'Source, 'Accumulate> -> 'Accumulate
Public Function Aggregate(Of TSource, TAccumulate) (source As IEnumerable(Of TSource), seed As TAccumulate, func As Func(Of TAccumulate, TSource, TAccumulate)) As TAccumulate
Type Parameters
- TSource
The type of the elements of source.
- TAccumulate
The type of the accumulator value.
Parameters
- source
-
IEnumerable
An IEnumerable
- seed
- TAccumulate
The initial accumulator value.
- func
-
Func
An accumulator function to be invoked on each element.
Returns
The final accumulator value.
Exceptions
source or func is null.
Examples
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.
Console.WriteLine($"The number of even integers is {numEven}")
End Sub
' This code produces the following output:
'
'The number of even integers is 6
Remarks
The Aggregatefunc one time for each element in source. Each time func is called, Aggregatefunc). The value of the seed parameter is used as the initial aggregate value. The result of func replaces the previous aggregated value. Aggregatefunc.
To 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.
Applies to
Aggregate(IEnumerable, Func)
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
- Source:
- Aggregate.cs
Applies an accumulator function over a sequence.
public:
generic
[System::Runtime::CompilerServices::Extension]
static TSource Aggregate(System::Collections::Generic::IEnumerable ^ source, Func ^ func);
public static TSource Aggregate(this System.Collections.Generic.IEnumerable source, Func func);
static member Aggregate : seq<'Source> * Func<'Source, 'Source, 'Source> -> 'Source
Public Function Aggregate(Of TSource) (source As IEnumerable(Of TSource), func As Func(Of TSource, TSource, TSource)) As TSource
Type Parameters
- TSource
The type of the elements of source.
Parameters
- source
-
IEnumerable
An IEnumerable
- func
-
Func
An accumulator function to be invoked on each element.
Returns
The final accumulator value.
Exceptions
source or func is null.
source contains no elements.
Examples
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.
Console.WriteLine(reversed)
End Sub
' This code produces the following output:
'
' dog lazy the over jumps fox brown quick the
Remarks
The Aggregatefunc one time for each element in source except the first one. Each time func is called, Aggregatefunc). The first element of source is used as the initial aggregate value. The result of func replaces the previous aggregated value. Aggregatefunc.
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. You should choose another overload if the return value should include only the elements of source that meet a certain condition. 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.
To 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.