Queryable.Aggregate Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Overload
| Nome | Descrizione |
|---|---|
| Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) |
Applica una funzione di enumeratore su una sequenza. Il valore di inizializzazione specificato viene utilizzato come valore dell'accumulatore iniziale e la funzione specificata viene usata per selezionare il valore del risultato. |
| Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) |
Applica una funzione di enumeratore su una sequenza. Il valore di inizializzazione specificato viene utilizzato come valore dell'caricabatterie iniziale. |
| Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) |
Applica una funzione di enumeratore su una sequenza. |
Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>)
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
Applica una funzione di enumeratore su una sequenza. Il valore di inizializzazione specificato viene utilizzato come valore dell'accumulatore iniziale e la funzione specificata viene usata per selezionare il valore del risultato.
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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.
- TAccumulate
Tipo del valore dell'accumulatore.
- TResult
Tipo del valore risultante.
Parametri
- source
- IQueryable<TSource>
Sequenza su cui aggregare.
- seed
- TAccumulate
Valore iniziale dell'accumulatore.
- func
- Expression<Func<TAccumulate,TSource,TAccumulate>>
Funzione enumerata da richiamare su ogni elemento.
- selector
- Expression<Func<TAccumulate,TResult>>
Funzione per trasformare il valore finale dell'accumulo nel valore del risultato.
Restituisce
Valore finale trasformato.
- Attributi
Eccezioni
source o o funcselector è null.
Esempio
Nell'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 enumerata e un selettore di risultati.
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 di tipo è uno dei Func<T,TResult> tipi . Per questi parametri, è possibile passare un'espressione lambda e verrà compilata in un oggetto Expression<TDelegate>.
Il Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,
TResult>>) metodo genera un oggetto MethodCallExpression che rappresenta la chiamata Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,
TResult>>) stessa come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al Execute<TResult>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro .
Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la 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. Il comportamento previsto è che la funzione specificata, func, viene applicata a ogni valore nella sequenza di origine e viene restituito il valore accumulato. Il seed parametro viene usato come valore di inizializzazione per il valore accumulato, che corrisponde al primo parametro in func. Il valore accumulato finale viene passato a per selector ottenere il valore del risultato.
Per semplificare le operazioni di aggregazione comuni, il set di operatori di query standard include anche due metodi di conteggio e Count , e LongCountquattro metodi di aggregazione numerica, ovvero Max, Min, Sume Average.
Si applica a
Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>)
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
Applica una funzione di enumeratore su una sequenza. Il valore di inizializzazione specificato viene utilizzato come valore dell'caricabatterie iniziale.
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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.
- TAccumulate
Tipo del valore dell'accumulatore.
Parametri
- source
- IQueryable<TSource>
Sequenza su cui aggregare.
- seed
- TAccumulate
Valore iniziale dell'accumulatore.
- func
- Expression<Func<TAccumulate,TSource,TAccumulate>>
Funzione enumerata da richiamare su ogni elemento.
Restituisce
Valore finale dell'caricabatterie.
- Attributi
Eccezioni
source o func è null.
Esempio
Nell'esempio di codice seguente viene illustrato come usare Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) per applicare una funzione enumerata quando viene fornito un valore di inizializzazione alla funzione.
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 di tipo è uno dei Func<T,TResult> tipi . Per questi parametri, è possibile passare un'espressione lambda e verrà compilata in un oggetto Expression<TDelegate>.
Il Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) metodo genera un oggetto MethodCallExpression che rappresenta la chiamata Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) stessa come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al Execute<TResult>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro .
Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che la funzione specificata, func, viene applicata a ogni valore nella sequenza di origine e viene restituito il valore accumulato. Il seed parametro viene usato come valore di inizializzazione per il valore accumulato, che corrisponde al primo parametro in func.
Per semplificare le operazioni di aggregazione comuni, il set di operatori di query standard include anche due metodi di conteggio e Count , e LongCountquattro metodi di aggregazione numerica, ovvero Max, Min, Sume Average.
Si applica a
Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>)
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
- Origine:
- Queryable.cs
Applica una funzione di enumeratore su una sequenza.
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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.
Parametri
- source
- IQueryable<TSource>
Sequenza su cui aggregare.
- func
- Expression<Func<TSource,TSource,TSource>>
Funzione dell'enumeratore da applicare a ogni elemento.
Restituisce
Valore finale dell'caricabatterie.
- Attributi
Eccezioni
source o func è null.
source non contiene elementi.
Esempio
Nell'esempio di codice seguente viene illustrato come usare Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) per compilare una frase da una matrice di stringhe.
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 di tipo è uno dei Func<T,TResult> tipi . Per questi parametri, è possibile passare un'espressione lambda e verrà compilata in un oggetto Expression<TDelegate>.
Il Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) metodo genera un oggetto MethodCallExpression che rappresenta la chiamata Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) stessa come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al Execute<TResult>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro .
Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che la funzione specificata, func, viene applicata a ogni valore nella sequenza di origine e viene restituito il valore accumulato. Il primo valore in source viene usato come valore di inizializzazione per il valore accumulato, che corrisponde al primo parametro in func.
Per semplificare le operazioni di aggregazione comuni, il set di operatori di query standard include anche due metodi di conteggio e Count , e LongCountquattro metodi di aggregazione numerica, ovvero Max, Min, Sume Average.