Enumerable.Aggregate Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Přetížení
| Name | Description |
|---|---|
| Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) |
Použije funkci akumulátoru na sekvenci. Zadaná počáteční hodnota akumulátoru se používá jako počáteční hodnota akumulátoru a zadaná funkce slouží k výběru výsledné hodnoty. |
| Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) |
Použije funkci akumulátoru na sekvenci. Zadaná počáteční hodnota akumulátoru se používá jako počáteční hodnota akumulátoru. |
| Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) |
Použije funkci akumulátoru na sekvenci. |
Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
Použije funkci akumulátoru na sekvenci. Zadaná počáteční hodnota akumulátoru se používá jako počáteční hodnota akumulátoru a zadaná funkce slouží k výběru výsledné hodnoty.
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 prvků .source
- TAccumulate
Typ akumulátoru.
- TResult
Typ výsledné hodnoty.
Parametry
- source
- IEnumerable<TSource>
Agregace IEnumerable<T> .
- seed
- TAccumulate
Počáteční hodnota akumulátoru.
- func
- Func<TAccumulate,TSource,TAccumulate>
Akumulátorová funkce, která má být vyvolána na každém prvku.
- resultSelector
- Func<TAccumulate,TResult>
Funkce, která transformuje konečnou hodnotu akumulátoru na výslednou hodnotu.
Návraty
Transformovaná konečná hodnota akumulátoru.
Výjimky
source nebo funcresultSelector je null.
Příklady
Následující příklad kódu ukazuje, jak použít Aggregate funkci akumulátoru a volič výsledků.
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
Poznámky
Tato Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) metoda usnadňuje provádění výpočtů v posloupnosti hodnot. Tato metoda funguje voláním func jednou pro každý prvek v source.
func Při každém zavolání Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) předá prvek z sekvence i agregovanou hodnotu (jako první argument).func Hodnota parametru seed se používá jako počáteční agregační hodnota. Výsledek func nahradí předchozí agregovanou hodnotu. Konečný výsledek func je předán k resultSelector získání konečného výsledku Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>).
Pro zjednodušení běžných agregačních operací zahrnují standardní operátory dotazů také metodu pro obecné účely a Countčtyři číselné metody agregace, konkrétně Min, , Max, Suma Average.
Platí pro
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
Použije funkci akumulátoru na sekvenci. Zadaná počáteční hodnota akumulátoru se používá jako počáteční hodnota akumulátoru.
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 prvků .source
- TAccumulate
Typ akumulátoru.
Parametry
- source
- IEnumerable<TSource>
Agregace IEnumerable<T> .
- seed
- TAccumulate
Počáteční hodnota akumulátoru.
- func
- Func<TAccumulate,TSource,TAccumulate>
Akumulátorová funkce, která má být vyvolána na každém prvku.
Návraty
Konečná hodnota akumulátoru.
Výjimky
source nebo func je null.
Příklady
Následující příklad kódu ukazuje, jak použít Aggregate funkci akumulátoru a použít počáteční hodnotu.
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
Poznámky
Tato Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) metoda usnadňuje provádění výpočtů v posloupnosti hodnot. Tato metoda funguje voláním func jednou pro každý prvek v source.
func Při každém zavolání Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) předá prvek z sekvence i agregovanou hodnotu (jako první argument).func Hodnota parametru seed se používá jako počáteční agregační hodnota. Výsledek func nahradí předchozí agregovanou hodnotu.
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) vrátí konečný výsledek func.
Pro zjednodušení běžných agregačních operací zahrnují standardní operátory dotazů také metodu pro obecné účely a Countčtyři číselné metody agregace, konkrétně Min, , Max, Suma Average.
Platí pro
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
- Zdroj:
- Aggregate.cs
Použije funkci akumulátoru na sekvenci.
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 prvků .source
Parametry
- source
- IEnumerable<TSource>
Agregace IEnumerable<T> .
- func
- Func<TSource,TSource,TSource>
Akumulátorová funkce, která má být vyvolána na každém prvku.
Návraty
Konečná hodnota akumulátoru.
Výjimky
source nebo func je null.
source neobsahuje žádné prvky.
Příklady
Následující příklad kódu ukazuje, jak obrátit pořadí slov v řetězci pomocí 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
Poznámky
Tato Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) metoda usnadňuje provádění výpočtů v posloupnosti hodnot. Tato metoda funguje voláním func jednou pro každý prvek kromě source prvního.
func Při každém zavolání Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) předá prvek z sekvence i agregovanou hodnotu (jako první argument).func První prvek source se používá jako počáteční agregační hodnota. Výsledek func nahradí předchozí agregovanou hodnotu.
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) vrátí konečný výsledek func.
Toto přetížení Aggregate metody není vhodné pro všechny případy, protože používá první prvek source jako počáteční agregační hodnotu. Pokud by návratová hodnota měla obsahovat pouze prvky source , které splňují určitou podmínku, měli byste zvolit jiné přetížení. Toto přetížení například není spolehlivé, pokud chcete vypočítat součet sudých čísel v source. Výsledek bude nesprávný, pokud je první prvek lichý, nikoli sudý.
Pro zjednodušení běžných agregačních operací zahrnují standardní operátory dotazů také metodu pro obecné účely a Countčtyři číselné metody agregace, konkrétně Min, , Max, Suma Average.