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ść inicjatora jest używana jako początkowa wartość akumulatorowa, a określona funkcja służy do wybierania wartości wyniku.

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

Stosuje funkcję akumulatora po sekwencji. Określona wartość inicjatora 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>)

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

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 elementu source.

TAccumulate

Typ wartości akumulatorowej.

TResult

Typ wartości wynikowej.

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 przekształcania końcowej wartości akumulatorowej w wartość wynikową.

Zwraca

TResult

Przekształcona końcowa wartość akumulatorowa.

Wyjątki

source lub func resultSelector ma wartość null.

Przykłady

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

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

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 przez wywołanie func raz dla każdego elementu w elemecie source. Za każdym razem func jest wywoływany element przekazuje Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) 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 zamienia poprzednią zagregowaną wartość. Końcowy wynik func jest przekazywany w resultSelector celu 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, Counti cztery metody agregacji liczbowej, a mianowicie Min, Max, Sumi Average.

Dotyczy

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

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

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 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 ma wartość null.

Przykłady

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

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

Metoda Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) ułatwia wykonywanie obliczeń w sekwencji wartości. Ta metoda działa przez wywołanie func raz dla każdego elementu w elemecie source. Za każdym razem func jest wywoływany element przekazuje Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) 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 zamienia poprzednią zagregowaną wartość. Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>) Zwraca wynik końcowy funkcji func.

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

Dotyczy

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

Stosuje funkcję akumulatora po sekwencji.

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 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 ma wartość 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.

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

Metoda Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) ułatwia wykonywanie obliczeń w sekwencji wartości. Ta metoda działa przez wywołanie func jeden raz dla każdego elementu source z wyjątkiem pierwszego. Za każdym razem func jest wywoływany element przekazuje Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) zarówno element z sekwencji, jak i zagregowaną wartość (jako pierwszy argument do func). Pierwszy element source elementu jest używany jako początkowa wartość agregacji. Wynik func zamienia poprzednią zagregowaną wartość. Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) Zwraca wynik końcowy funkcji 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. Jeśli zwracana wartość powinna zawierać tylko elementy source spełniające określony warunek, należy wybrać inne przeciążenie. Na przykład to przeciążenie nie jest niezawodne, jeśli chcesz obliczyć sumę liczb parzysowych 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, Counti cztery metody agregacji liczbowej, a mianowicie Min, Max, Sumi Average.

Dotyczy