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. Określona wartość inicjowania jest używana jako początkowa wartość akumulatora, a określona funkcja jest używana do wybierania wartości wyniku.

Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)

Stosuje funkcję akumulatora po sekwencji. Określona wartość nasion jest używana jako początkowa wartość akumulatorowa.

Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)

Stosuje funkcję akumulatora po sekwencji.

Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)

Źródło:
Aggregate.cs
Źródło:
Aggregate.cs
Źródło:
Aggregate.cs

Stosuje funkcję akumulatora po sekwencji. Określona wartość inicjowania jest używana jako początkowa wartość akumulatora, a określona funkcja jest używana do wybierania wartości wyniku.

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

Parametry typu

TSource

Typ elementów elementu source.

TAccumulate

Typ wartości akumulatorowej.

TResult

Typ wynikowej wartości.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> do zagregowania.

seed
TAccumulate

Początkowa wartość akumulatorowa.

func
Func<TAccumulate,TSource,TAccumulate>

Funkcja akumulatorowa, która ma być wywoływana na każdym elemecie.

resultSelector
Func<TAccumulate,TResult>

Funkcja umożliwiająca przekształcenie ostatecznej wartości akumulatorowej na wartość wynikową.

Zwraca

TResult

Przekształcona wartość końcowego akumulatora.

Wyjątki

source lub funcresultSelector jest null.

Przykłady

W poniższym przykładzie kodu pokazano, jak zastosować Aggregate funkcję akumulatorową i selektor wyników.

C#
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.

Uwagi

Metoda Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) ułatwia wykonywanie obliczeń w sekwencji wartości. Ta metoda działa, wywołując func jeden raz dla każdego elementu w elemecie source. Za każdym razem func wywoływany Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) element przekazuje zarówno element z sekwencji, jak i zagregowaną wartość (jako pierwszy argument do func). Wartość parametru seed jest używana jako początkowa wartość agregacji. Wynik func zastępuje poprzednią zagregowaną wartość. Ostateczny wynik func jest przekazywany w celu resultSelector uzyskania końcowego wyniku .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ę ogólnego przeznaczenia, oraz cztery metody agregacji liczbowej, Countczyli Min, Max, Sumi Average.

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)

Źródło:
Aggregate.cs
Źródło:
Aggregate.cs
Źródło:
Aggregate.cs

Stosuje funkcję akumulatora po sekwencji. Określona wartość nasion jest używana jako początkowa wartość akumulatorowa.

C#
public static TAccumulate Aggregate<TSource,TAccumulate> (this System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func);

Parametry typu

TSource

Typ elementów elementu source.

TAccumulate

Typ wartości akumulatorowej.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> do zagregowania.

seed
TAccumulate

Początkowa wartość akumulatorowa.

func
Func<TAccumulate,TSource,TAccumulate>

Funkcja akumulatorowa, która ma być wywoływana na każdym elemecie.

Zwraca

TAccumulate

Końcowa wartość akumulatorowa.

Wyjątki

source lub func to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak zastosować Aggregate funkcję akumulatorową i użyć wartości nasion.

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

Uwagi

Metoda Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) ułatwia wykonywanie obliczeń w sekwencji wartości. Ta metoda działa, wywołując func jeden raz dla każdego elementu w elemecie source. Za każdym razem func wywoływany Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) element przekazuje zarówno element z sekwencji, jak i zagregowaną wartość (jako pierwszy argument do func). Wartość parametru seed jest używana jako początkowa wartość agregacji. Wynik func zastępuje poprzednią zagregowaną wartość. Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)zwraca wynik końcowy .func

Aby uprościć typowe operacje agregacji, standardowe operatory zapytań obejmują również metodę ogólnego przeznaczenia, oraz cztery metody agregacji liczbowej, Countczyli Min, Max, Sumi Average.

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)

Źródło:
Aggregate.cs
Źródło:
Aggregate.cs
Źródło:
Aggregate.cs

Stosuje funkcję akumulatora po sekwencji.

C#
public static TSource Aggregate<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TSource,TSource> func);

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> do zagregowania.

func
Func<TSource,TSource,TSource>

Funkcja akumulatorowa, która ma być wywoływana na każdym elemecie.

Zwraca

TSource

Końcowa wartość akumulatorowa.

Wyjątki

source lub func to null.

source nie zawiera żadnych elementów.

Przykłady

W poniższym przykładzie kodu pokazano, jak odwrócić kolejność wyrazów w ciągu przy użyciu polecenia Aggregate.

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

Uwagi

Metoda Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) ułatwia wykonywanie obliczeń w sekwencji wartości. Ta metoda działa, wywołując func jeden raz dla każdego elementu z source wyjątkiem pierwszego. Za każdym razem func wywoływany Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) element przekazuje zarówno element z sekwencji, jak i zagregowaną wartość (jako pierwszy argument do func). Pierwszy element source jest używany jako początkowa wartość agregacji. Wynik func zastępuje poprzednią zagregowaną wartość. Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)zwraca wynik końcowy .func

To przeciążenie Aggregate metody nie jest odpowiednie dla wszystkich przypadków, ponieważ używa pierwszego elementu source jako początkowej wartości zagregowanej. Należy wybrać inne przeciążenie, jeśli wartość zwracana powinna zawierać tylko elementy source spełniające określony warunek. Na przykład to przeciążenie nie jest niezawodne, jeśli chcesz obliczyć sumę liczb parzysłych w elemecie source. Wynik będzie niepoprawny, jeśli pierwszy element jest nieparzysły, a nie równy.

Aby uprościć typowe operacje agregacji, standardowe operatory zapytań obejmują również metodę ogólnego przeznaczenia, oraz cztery metody agregacji liczbowej, Countczyli Min, Max, Sumi Average.

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0