Enumerable.Aggregate Metoda
Definicja
Przeciążenia
Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) |
Stosuje funkcję akumulatora po sekwencji.Applies an accumulator function over a sequence. Określona wartość inicjatora jest używana jako początkowa wartość akumulowana, a określona funkcja jest używana do wybierania wartości wyniku.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>) |
Stosuje funkcję akumulatora po sekwencji.Applies an accumulator function over a sequence. Określona wartość inicjatora jest używana jako początkowa wartość akumulowana.The specified seed value is used as the initial accumulator value. |
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) |
Stosuje funkcję akumulatora po sekwencji.Applies an accumulator function over a sequence. |
Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)
Stosuje funkcję akumulatora po sekwencji.Applies an accumulator function over a sequence. Określona wartość inicjatora jest używana jako początkowa wartość akumulowana, a określona funkcja jest używana do wybierania wartości wyniku.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
Parametry typu
- TSource
Typ elementów source
.The type of the elements of source
.
- TAccumulate
Typ wartości elementu akumulowana.The type of the accumulator value.
- TResult
Typ wartości otrzymanej.The type of the resulting value.
Parametry
- source
- IEnumerable<TSource>
IEnumerable<T>Do agregacji.An IEnumerable<T> to aggregate over.
- seed
- TAccumulate
Początkowa wartość akumulowana.The initial accumulator value.
- func
- Func<TAccumulate,TSource,TAccumulate>
Funkcja akumulowana, która ma być wywoływana dla każdego elementu.An accumulator function to be invoked on each element.
- resultSelector
- Func<TAccumulate,TResult>
Funkcja Przekształć końcową wartość akumulowana na wartość wynikową.A function to transform the final accumulator value into the result value.
Zwraca
- TResult
Przekształcona wartość końcowego akumulowana.The transformed final accumulator value.
Wyjątki
source
or func
lub resultSelector
is null
.source
or func
or resultSelector
is null
.
Przykłady
Poniższy przykład kodu demonstruje, jak używać Aggregate do zastosowania funkcji i selektora wyników.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
Uwagi
Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)Metoda ułatwia wykonywanie obliczeń na sekwencji wartości.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. Ta metoda działa przez wywoływanie func
jednokrotnie dla każdego elementu w source
.This method works by calling func
one time for each element in source
. Każdy moment func
jest wywoływany, Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) przekazuje zarówno element z sekwencji, jak i agregowaną wartość (jako pierwszy argument do 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
). Wartość seed
parametru jest używana jako początkowa wartość agregowania.The value of the seed
parameter is used as the initial aggregate value. Wynik func
zastąpienia poprzedniej wartości agregowanej.The result of func
replaces the previous aggregated value. Końcowy wynik func
jest przekazanie do w resultSelector
celu uzyskania końcowego wyniku 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>).
Aby uprościć Typowe operacje agregacji, standardowe operatory zapytań obejmują również metodę zliczania ogólnego zastosowania, Count i cztery metody agregacji liczbowej, mianowicie Min ,, Max Sum , i Average .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.
Dotyczy
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)
Stosuje funkcję akumulatora po sekwencji.Applies an accumulator function over a sequence. Określona wartość inicjatora jest używana jako początkowa wartość akumulowana.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
Parametry typu
- TSource
Typ elementów source
.The type of the elements of source
.
- TAccumulate
Typ wartości elementu akumulowana.The type of the accumulator value.
Parametry
- source
- IEnumerable<TSource>
IEnumerable<T>Do agregacji.An IEnumerable<T> to aggregate over.
- seed
- TAccumulate
Początkowa wartość akumulowana.The initial accumulator value.
- func
- Func<TAccumulate,TSource,TAccumulate>
Funkcja akumulowana, która ma być wywoływana dla każdego elementu.An accumulator function to be invoked on each element.
Zwraca
- TAccumulate
Końcowa wartość akumulowana.The final accumulator value.
Wyjątki
source
lub func
jest null
.source
or func
is null
.
Przykłady
Poniższy przykład kodu demonstruje, jak używać Aggregate do zastosowania funkcji akumulowana i używania wartości inicjatora.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
Uwagi
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)Metoda ułatwia wykonywanie obliczeń na sekwencji wartości.The Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) method makes it simple to perform a calculation over a sequence of values. Ta metoda działa przez wywoływanie func
jednokrotnie dla każdego elementu w source
.This method works by calling func
one time for each element in source
. Każdy moment func
jest wywoływany, Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) przekazuje zarówno element z sekwencji, jak i agregowaną wartość (jako pierwszy argument do 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
). Wartość seed
parametru jest używana jako początkowa wartość agregowania.The value of the seed
parameter is used as the initial aggregate value. Wynik func
zastąpienia poprzedniej wartości agregowanej.The result of func
replaces the previous aggregated value. Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) zwraca końcowy wynik func
.Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) returns the final result of func
.
Aby uprościć Typowe operacje agregacji, standardowe operatory zapytań obejmują również metodę zliczania ogólnego zastosowania, Count i cztery metody agregacji liczbowej, mianowicie Min ,, Max Sum , i Average .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.
Dotyczy
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)
Stosuje funkcję akumulatora po sekwencji.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
Parametry typu
- TSource
Typ elementów source
.The type of the elements of source
.
Parametry
- source
- IEnumerable<TSource>
IEnumerable<T>Do agregacji.An IEnumerable<T> to aggregate over.
- func
- Func<TSource,TSource,TSource>
Funkcja akumulowana, która ma być wywoływana dla każdego elementu.An accumulator function to be invoked on each element.
Zwraca
- TSource
Końcowa wartość akumulowana.The final accumulator value.
Wyjątki
source
lub func
jest null
.source
or func
is null
.
source
nie zawiera żadnych elementów.source
contains no elements.
Przykłady
Poniższy przykład kodu demonstruje sposób odwrócenia kolejności wyrazów w ciągu przy użyciu 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.
Console.WriteLine(reversed)
End Sub
' This code produces the following output:
'
' dog lazy the over jumps fox brown quick the
Uwagi
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)Metoda ułatwia wykonywanie obliczeń na sekwencji wartości.The Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) method makes it simple to perform a calculation over a sequence of values. Ta metoda działa przez wywoływanie func
jednokrotnie dla każdego elementu w source
z wyjątkiem pierwszego.This method works by calling func
one time for each element in source
except the first one. Każdy moment func
jest wywoływany, Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) przekazuje zarówno element z sekwencji, jak i agregowaną wartość (jako pierwszy argument do 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
). Pierwszy element source
jest używany jako początkowa wartość agregowania.The first element of source
is used as the initial aggregate value. Wynik func
zastąpienia poprzedniej wartości agregowanej.The result of func
replaces the previous aggregated value. Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) zwraca końcowy wynik func
.Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) returns the final result of func
.
To Przeciążenie Aggregate metody nie jest odpowiednie dla wszystkich przypadków, ponieważ używa pierwszego elementu source
jako początkowej wartości agregowanej.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. Należy wybrać inne Przeciążenie, jeśli wartość zwracana powinna zawierać tylko elementy source
, które spełniają określony warunek.You should choose another overload if the return value should include only the elements of source
that meet a certain condition. Na przykład to Przeciążenie nie jest niezawodne, jeśli chcesz obliczyć sumę parzystych liczb w source
.For example, this overload isn't reliable if you want to calculate the sum of the even numbers in source
. Wynik będzie nieprawidłowy, jeśli pierwszy element jest nieparzysty, a nie nawet.The result will be incorrect if the first element is odd instead of even.
Aby uprościć Typowe operacje agregacji, standardowe operatory zapytań obejmują również metodę zliczania ogólnego zastosowania, Count i cztery metody agregacji liczbowej, mianowicie Min ,, Max Sum , i Average .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.