Queryable.Aggregate Metoda

Definicja

Przeciążenia

Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<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>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>)

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

Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>)

Stosuje funkcję akumulatora po sekwencji.

Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.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.

public:
generic <typename TSource, typename TAccumulate, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static TResult Aggregate(System::Linq::IQueryable<TSource> ^ source, TAccumulate seed, System::Linq::Expressions::Expression<Func<TAccumulate, TSource, TAccumulate> ^> ^ func, System::Linq::Expressions::Expression<Func<TAccumulate, TResult> ^> ^ selector);
public static TResult Aggregate<TSource,TAccumulate,TResult> (this System.Linq.IQueryable<TSource> source, TAccumulate seed, System.Linq.Expressions.Expression<Func<TAccumulate,TSource,TAccumulate>> func, System.Linq.Expressions.Expression<Func<TAccumulate,TResult>> selector);
static member Aggregate : System.Linq.IQueryable<'Source> * 'Accumulate * System.Linq.Expressions.Expression<Func<'Accumulate, 'Source, 'Accumulate>> * System.Linq.Expressions.Expression<Func<'Accumulate, 'Result>> -> 'Result
<Extension()>
Public Function Aggregate(Of TSource, TAccumulate, TResult) (source As IQueryable(Of TSource), seed As TAccumulate, func As Expression(Of Func(Of TAccumulate, TSource, TAccumulate)), selector As Expression(Of Func(Of TAccumulate, TResult))) As TResult

Parametry typu

TSource

Typ elementów elementu source.

TAccumulate

Typ wartości akumulatorowej.

TResult

Typ wynikowej wartości.

Parametry

source
IQueryable<TSource>

Sekwencja do zagregowania.

seed
TAccumulate

Początkowa wartość akumulatorowa.

func
Expression<Func<TAccumulate,TSource,TAccumulate>>

Funkcja akumulatorowa do wywoływania dla każdego elementu.

selector
Expression<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 funcselector jest null.

Przykłady

W poniższym przykładzie kodu pokazano, jak zastosować Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) funkcję akumulatorową 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.AsQueryable().Aggregate(
    "banana",
    (longest, next) => next.Length > longest.Length ? next : longest,
    // Return the final result as an uppercase 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.
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.AsQueryable().Aggregate( _
    "banana", _
    Function(ByVal longest, ByVal fruit) IIf(fruit.Length > longest.Length, fruit, longest), _
    Function(ByVal fruit) fruit.ToUpper() _
)

MsgBox(String.Format( _
    "The fruit with the longest name is {0}.", longestName) _
)

' This code produces the following output:
'
' The fruit with the longest name is PASSIONFRUIT.

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. W przypadku tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) generuje element MethodCallExpression , który reprezentuje wywołanie Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do Execute<TResult>(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że określona funkcja , funcjest stosowana do każdej wartości w sekwencji źródłowej i zwracana jest skumulowana wartość. Parametr seed jest używany jako wartość inicjującą dla skumulowanej wartości, która odpowiada pierwszemu parametrowi w elemencie func. Końcowa skumulowana wartość jest przekazywana w celu selector uzyskania wartości wyniku.

Aby uprościć typowe operacje agregacji, zestaw standardowych operatorów zapytań obejmuje również dwie metody zliczania, oraz cztery metody agregacji liczbowej, Count a mianowicie Max, Min, Sumi Average.LongCount

Dotyczy

Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>)

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

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

public:
generic <typename TSource, typename TAccumulate>
[System::Runtime::CompilerServices::Extension]
 static TAccumulate Aggregate(System::Linq::IQueryable<TSource> ^ source, TAccumulate seed, System::Linq::Expressions::Expression<Func<TAccumulate, TSource, TAccumulate> ^> ^ func);
public static TAccumulate Aggregate<TSource,TAccumulate> (this System.Linq.IQueryable<TSource> source, TAccumulate seed, System.Linq.Expressions.Expression<Func<TAccumulate,TSource,TAccumulate>> func);
static member Aggregate : System.Linq.IQueryable<'Source> * 'Accumulate * System.Linq.Expressions.Expression<Func<'Accumulate, 'Source, 'Accumulate>> -> 'Accumulate
<Extension()>
Public Function Aggregate(Of TSource, TAccumulate) (source As IQueryable(Of TSource), seed As TAccumulate, func As Expression(Of Func(Of TAccumulate, TSource, TAccumulate))) As TAccumulate

Parametry typu

TSource

Typ elementów elementu source.

TAccumulate

Typ wartości akumulatorowej.

Parametry

source
IQueryable<TSource>

Sekwencja do zagregowania.

seed
TAccumulate

Początkowa wartość akumulatorowa.

func
Expression<Func<TAccumulate,TSource,TAccumulate>>

Funkcja akumulatorowa do wywoływania dla każdego elementu.

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<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) funkcję akumulatorową, gdy do funkcji zostanie podana wartość inicjowania.

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.AsQueryable().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
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.AsQueryable().Aggregate( _
        0, _
        Function(ByVal total, ByVal number) _
            IIf(number Mod 2 = 0, total + 1, total) _
    )

MsgBox(String.Format("The number of even integers is: {0}", numEven))

' This code produces the following output:
'
' The number of even integers is: 6

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. W przypadku tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) generuje element MethodCallExpression , który reprezentuje wywołanie Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do Execute<TResult>(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że określona funkcja , funcjest stosowana do każdej wartości w sekwencji źródłowej i zwracana jest skumulowana wartość. Parametr seed jest używany jako wartość inicjującą dla skumulowanej wartości, która odpowiada pierwszemu parametrowi w elemencie func.

Aby uprościć typowe operacje agregacji, zestaw standardowych operatorów zapytań obejmuje również dwie metody zliczania, oraz cztery metody agregacji liczbowej, Count a mianowicie Max, Min, Sumi Average.LongCount

Dotyczy

Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>)

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

Stosuje funkcję akumulatora po sekwencji.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource Aggregate(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TSource, TSource> ^> ^ func);
public static TSource Aggregate<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TSource,TSource>> func);
static member Aggregate : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Source, 'Source>> -> 'Source
<Extension()>
Public Function Aggregate(Of TSource) (source As IQueryable(Of TSource), func As Expression(Of Func(Of TSource, TSource, TSource))) As TSource

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Sekwencja do zagregowania.

func
Expression<Func<TSource,TSource,TSource>>

Funkcja akumulatorowa, która ma być stosowana do każdego elementu.

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 utworzyć Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) zdanie z tablicy ciągów.

string sentence = "the quick brown fox jumps over the lazy dog";

// Split the string into individual words.
string[] words = sentence.Split(' ');

// Use Aggregate() to prepend each word to the beginning of the
// new sentence to reverse the word order.
string reversed =
    words.AsQueryable().Aggregate(
    (workingSentence, next) => next + " " + workingSentence
    );

Console.WriteLine(reversed);

// This code produces the following output:
//
// dog lazy the over jumps fox brown quick the
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)

' Use Aggregate() to prepend each word to the beginning of the 
' new sentence to reverse the word order.
Dim reversed As String = _
    words.AsQueryable().Aggregate( _
        Function(ByVal workingSentence, ByVal nextWord) nextWord & " " & workingSentence _
    )

MsgBox(reversed)

' This code produces the following output:
'
' dog lazy the over jumps fox brown quick the

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. W przypadku tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) generuje element MethodCallExpression , który reprezentuje wywołanie Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do Execute<TResult>(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że określona funkcja , funcjest stosowana do każdej wartości w sekwencji źródłowej i zwracana jest skumulowana wartość. Pierwsza wartość w source pliku jest używana jako wartość inicjującą dla skumulowanej wartości, która odpowiada pierwszemu parametrowi w funcelemencie .

Aby uprościć typowe operacje agregacji, zestaw standardowych operatorów zapytań obejmuje również dwie metody zliczania, oraz cztery metody agregacji liczbowej, Count a mianowicie Max, Min, Sumi Average.LongCount

Dotyczy