Queryable.Aggregate Queryable.Aggregate Queryable.Aggregate Queryable.Aggregate Method

Definizione

Overload

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

Applica una funzione accumulatore una sequenza.Applies an accumulator function over a sequence. Il valore di inizializzazione specificato viene usato come valore iniziale dell'accumulatore e la funzione specificata viene usata per selezionare il valore risultante.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>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>)

Applica una funzione accumulatore una sequenza.Applies an accumulator function over a sequence. Il valore di inizializzazione specificato viene usato come valore iniziale dell'accumulatore.The specified seed value is used as the initial accumulator value.

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

Applica una funzione accumulatore una sequenza.Applies an accumulator function over a sequence.

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

Applica una funzione accumulatore una sequenza.Applies an accumulator function over a sequence. Il valore di inizializzazione specificato viene usato come valore iniziale dell'accumulatore e la funzione specificata viene usata per selezionare il valore risultante.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::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

Parametri di tipo

TSource

Tipo degli elementi di source.The type of the elements of source.

TAccumulate

Tipo del valore dell'accumulatore.The type of the accumulator value.

TResult

Il tipo del valore risultante.The type of the resulting value.

Parametri

source
IQueryable<TSource>

Una sequenza su cui aggregare.A sequence to aggregate over.

seed
TAccumulate TAccumulate TAccumulate TAccumulate

Valore iniziale dell'accumulatore.The initial accumulator value.

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

Funzione accumulatore da richiamare su ogni elemento.An accumulator function to invoke on each element.

selector
Expression<Func<TAccumulate,TResult>>

Una funzione per trasformare il valore finale dell'accumulatore nel valore risultante.A function to transform the final accumulator value into the result value.

Restituisce

TResult TResult TResult TResult

Il valore finale trasformato dell'accumulatore.The transformed final accumulator value.

Eccezioni

Il parametro source, il parametro func o il parametro selector è null.source or func or selector is null.

Esempi

Esempio di codice seguente viene illustrato come utilizzare Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>) per applicare una funzione accumulatore e un selettore di risultato.The following code example demonstrates how to use Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>) 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.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. 

Commenti

Questo metodo ha almeno un parametro di tipo Expression<TDelegate> il cui argomento tipo è uno del Func<T,TResult> tipi.This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. Per questi parametri, è possibile passare in un'espressione lambda e verrà compilata per un Expression<TDelegate>.For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

Il Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>) metodo genera un MethodCallExpression che rappresenta una chiamata Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>) stesso come un metodo generico costruito.The Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>) method generates a MethodCallExpression that represents calling Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>) itself as a constructed generic method. Passa quindi il MethodCallExpression per il Execute<TResult>(Expression) metodo del IQueryProvider rappresentato dal Provider proprietà del source parametro.It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

Il comportamento delle query che si verifica in seguito all'esecuzione di un albero delle espressioni che rappresenta una chiamata Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>) dipende dall'implementazione del tipo del source parametro.The query behavior that occurs as a result of executing an expression tree that represents calling Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>) depends on the implementation of the type of the source parameter. Il comportamento previsto è che la funzione specificata, func, viene applicato a ogni valore nella sequenza di origine e di accumulato valore viene restituito.The expected behavior is that the specified function, func, is applied to each value in the source sequence and the accumulated value is returned. Il seed parametro viene utilizzato come valore di inizializzazione per il valore accumulato, che corrisponde al primo parametro in func.The seed parameter is used as the seed value for the accumulated value, which corresponds to the first parameter in func. Il valore accumulato finale viene passato a selector per ottenere il valore del risultato.The final accumulated value is passed to selector to obtain the result value.

Per semplificare le comuni operazioni di aggregazione, il set di operatori di query standard include anche due metodi di conteggio, Count e LongCounte quattro i metodi di aggregazione numeriche, vale a dire Max, Min, Sume Average.To simplify common aggregation operations, the set of standard query operators also includes two counting methods, Count and LongCount, and four numeric aggregation methods, namely Max, Min, Sum, and Average.

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

Applica una funzione accumulatore una sequenza.Applies an accumulator function over a sequence. Il valore di inizializzazione specificato viene usato come valore iniziale dell'accumulatore.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::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

Parametri di tipo

TSource

Tipo degli elementi di source.The type of the elements of source.

TAccumulate

Tipo del valore dell'accumulatore.The type of the accumulator value.

Parametri

source
IQueryable<TSource>

Una sequenza su cui aggregare.A sequence to aggregate over.

seed
TAccumulate TAccumulate TAccumulate TAccumulate

Valore iniziale dell'accumulatore.The initial accumulator value.

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

Funzione accumulatore da richiamare su ogni elemento.An accumulator function to invoke on each element.

Restituisce

TAccumulate TAccumulate TAccumulate TAccumulate

Valore finale dell'accumulatore.The final accumulator value.

Eccezioni

Esempi

Esempio di codice seguente viene illustrato come utilizzare Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) per applicare una funzione accumulatore quando un valore di inizializzazione viene fornito alla funzione.The following code example demonstrates how to use Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) to apply an accumulator function when a seed value is provided to the function.

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 

Commenti

Questo metodo ha almeno un parametro di tipo Expression<TDelegate> il cui argomento tipo è uno del Func<T,TResult> tipi.This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. Per questi parametri, è possibile passare in un'espressione lambda e verrà compilata per un Expression<TDelegate>.For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

Il Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) metodo genera un MethodCallExpression che rappresenta una chiamata Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) stesso come un metodo generico costruito.The Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) method generates a MethodCallExpression that represents calling Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) itself as a constructed generic method. Passa quindi il MethodCallExpression per il Execute<TResult>(Expression) metodo del IQueryProvider rappresentato dal Provider proprietà del source parametro.It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

Il comportamento delle query che si verifica in seguito all'esecuzione di un albero delle espressioni che rappresenta una chiamata Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) dipende dall'implementazione del tipo del source parametro.The query behavior that occurs as a result of executing an expression tree that represents calling Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) depends on the implementation of the type of the source parameter. Il comportamento previsto è che la funzione specificata, func, viene applicato a ogni valore nella sequenza di origine e di accumulato valore viene restituito.The expected behavior is that the specified function, func, is applied to each value in the source sequence and the accumulated value is returned. Il seed parametro viene utilizzato come valore di inizializzazione per il valore accumulato, che corrisponde al primo parametro in func.The seed parameter is used as the seed value for the accumulated value, which corresponds to the first parameter in func.

Per semplificare le comuni operazioni di aggregazione, il set di operatori di query standard include anche due metodi di conteggio, Count e LongCounte quattro i metodi di aggregazione numeriche, vale a dire Max, Min, Sume Average.To simplify common aggregation operations, the set of standard query operators also includes two counting methods, Count and LongCount, and four numeric aggregation methods, namely Max, Min, Sum, and Average.

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

Applica una funzione accumulatore una sequenza.Applies an accumulator function over a sequence.

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

Parametri di tipo

TSource

Tipo degli elementi di source.The type of the elements of source.

Parametri

source
IQueryable<TSource>

Una sequenza su cui aggregare.A sequence to aggregate over.

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

Funzione accumulatore da applicare a ogni elemento.An accumulator function to apply to each element.

Restituisce

TSource TSource TSource TSource

Valore finale dell'accumulatore.The final accumulator value.

Eccezioni

Esempi

Esempio di codice seguente viene illustrato come utilizzare Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) per compilare una frase da una matrice di stringhe.The following code example demonstrates how to use Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) to build a sentence from an array of strings.

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

Commenti

Questo metodo ha almeno un parametro di tipo Expression<TDelegate> il cui argomento tipo è uno del Func<T,TResult> tipi.This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. Per questi parametri, è possibile passare in un'espressione lambda e verrà compilata per un Expression<TDelegate>.For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

Il Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) metodo genera un MethodCallExpression che rappresenta una chiamata Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) stesso come un metodo generico costruito.The Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) method generates a MethodCallExpression that represents calling Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) itself as a constructed generic method. Passa quindi il MethodCallExpression per il Execute<TResult>(Expression) metodo del IQueryProvider rappresentato dal Provider proprietà del source parametro.It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

Il comportamento delle query che si verifica in seguito all'esecuzione di un albero delle espressioni che rappresenta una chiamata Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) dipende dall'implementazione del tipo del source parametro.The query behavior that occurs as a result of executing an expression tree that represents calling Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) depends on the implementation of the type of the source parameter. Il comportamento previsto è che la funzione specificata, func, viene applicato a ogni valore nella sequenza di origine e di accumulato valore viene restituito.The expected behavior is that the specified function, func, is applied to each value in the source sequence and the accumulated value is returned. Il primo valore in source viene usato come valore di inizializzazione per il valore accumulato, che corrisponde al primo parametro in func.The first value in source is used as the seed value for the accumulated value, which corresponds to the first parameter in func.

Per semplificare le comuni operazioni di aggregazione, il set di operatori di query standard include anche due metodi di conteggio, Count e LongCounte quattro i metodi di aggregazione numeriche, vale a dire Max, Min, Sume Average.To simplify common aggregation operations, the set of standard query operators also includes two counting methods, Count and LongCount, and four numeric aggregation methods, namely Max, Min, Sum, and Average.

Si applica a