{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The Aggregate operator applies a function over a sequence. The result of the previous call to the function is passed in as an argument to the function with the next element. Optionally starts with a seed value and select projection clause.
EditMethod Signatures
// 1 - Call Function on Each Element
public static TSource Aggregate<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> func)
// 2 - With Seed
public static TAccumulate Aggregate<TSource, TAccumulate>(
this IEnumerable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func)
// 3 - With Seed and Select Projection
public static TResult Aggregate<TSource, TAccumulate, TResult>(
this IEnumerable<TSource> source,
TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func,
Func<TAccumulate, TResult> resultSelector)EditExceptions
Throws an ArgumentNullException if
source or
func or
resultSelector is null.
Throws an InvalidOperationException if
source is empty.
EditPseudo-code
If
source is null throw ArgumentNullException.
If
func is null throw ArgumentNullException.
If
source is empty throw an InvalidOperationException.
Overload 1:
Start with the first element (first value of result).
Iterate over the
source elements (starting at the second element if its there, otherwise break out of this loop)
result = func( previous result, this element )
Return result.
Overload 2:
Start with the
seed value (first value of result)
Iterate over the
source elements (starting at the first element)
result = func( previous result, this element )
Return result.
Overload 3:
Start with the
seed value (first value of result)
Iterate over the
source elements (starting at the first element)
result = func( previous result, this element )
Pass in the result to the
resultSelector function and return its result.
EditLoop Count
1 (the source sequence is always iterated one time)
EditCode Samples
TODO: Code sample needed.