Queryable.Aggregate Metoda

Definice

Přetížení

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

Použije na sekvenci funkci akumulátoru. Zadaná počáteční hodnota se použije jako počáteční hodnota akumulátoru a zadaná funkce se použije k výběru výsledné hodnoty.

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

Použije na sekvenci funkci akumulátoru. Zadaná počáteční hodnota se použije jako počáteční hodnota akumulátoru.

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

Použije na sekvenci funkci akumulátoru.

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Použije na sekvenci funkci akumulátoru. Zadaná počáteční hodnota se použije jako počáteční hodnota akumulátoru a zadaná funkce se použije k výběru výsledné hodnoty.

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 prvků objektu source.

TAccumulate

Typ hodnoty akumulátoru.

TResult

Typ výsledné hodnoty.

Parametry

source
IQueryable<TSource>

Posloupnost, která se má agregovat.

seed
TAccumulate

Počáteční hodnota akumulátoru.

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

Funkce akumulátoru, která se má vyvolat na každém prvku.

selector
Expression<Func<TAccumulate,TResult>>

Funkce pro transformaci konečné hodnoty akumulátoru na výslednou hodnotu.

Návraty

TResult

Transformovaná konečná hodnota akumulátoru.

Výjimky

source nebo funcselector je null.

Příklady

Následující příklad kódu ukazuje použití Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) funkce akumulátoru a selektoru výsledků.

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.

Poznámky

Tato metoda má alespoň jeden parametr typu Expression<TDelegate> , jehož argument typu je jedním z Func<T,TResult> typů. Pro tyto parametry můžete předat výraz lambda, který se zkompiluje do Expression<TDelegate>.

Metoda Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) vygeneruje MethodCallExpression , který představuje volání Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) sebe sama jako konstruované obecné metody. Pak předá MethodCallExpressionExecute<TResult>(Expression) metodě parametru IQueryProvider reprezentované Provider vlastností source .

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, který představuje volání Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) , závisí na implementaci typu parametru source . Očekávané chování je, funcže se zadaná funkce použije na každou hodnotu ve zdrojové sekvenci a vrátí se kumulovaná hodnota. Parametr se seed používá jako počáteční hodnota kumulované hodnoty, která odpovídá prvnímu parametru v funcsouboru . Konečná kumulovaná hodnota se předá do selector , aby se získala výsledná hodnota.

Pro zjednodušení běžných agregačních operací zahrnuje sada standardních operátorů dotazu také dvě metody Count počítání a a LongCountčtyři metody číselné agregace, konkrétně Max, MinSum, a Average.

Platí pro

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Použije na sekvenci funkci akumulátoru. Zadaná počáteční hodnota se použije jako počáteční hodnota akumulátoru.

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 prvků objektu source.

TAccumulate

Typ hodnoty akumulátoru.

Parametry

source
IQueryable<TSource>

Posloupnost, která se má agregovat.

seed
TAccumulate

Počáteční hodnota akumulátoru.

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

Funkce akumulátoru, která se má vyvolat na každém prvku.

Návraty

TAccumulate

Konečná hodnota akumulátoru.

Výjimky

source nebo func je null.

Příklady

Následující příklad kódu ukazuje, jak použít Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) funkci akumulátoru, když je do funkce poskytnuta počáteční hodnota.

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

Poznámky

Tato metoda má alespoň jeden parametr typu Expression<TDelegate> , jehož argument typu je jedním z Func<T,TResult> typů. Pro tyto parametry můžete předat výraz lambda, který se zkompiluje do Expression<TDelegate>.

Metoda Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) vygeneruje MethodCallExpression , který představuje volání Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) sebe sama jako konstruované obecné metody. Pak předá MethodCallExpressionExecute<TResult>(Expression) metodě parametru IQueryProvider reprezentované Provider vlastností source .

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, který představuje volání Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) , závisí na implementaci typu parametru source . Očekávané chování je, funcže se zadaná funkce použije na každou hodnotu ve zdrojové sekvenci a vrátí se kumulovaná hodnota. Parametr se seed používá jako počáteční hodnota kumulované hodnoty, která odpovídá prvnímu parametru v funcsouboru .

Pro zjednodušení běžných agregačních operací zahrnuje sada standardních operátorů dotazu také dvě metody Count počítání a a LongCountčtyři metody číselné agregace, konkrétně Max, MinSum, a Average.

Platí pro

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Použije na sekvenci funkci akumulátoru.

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 prvků objektu source.

Parametry

source
IQueryable<TSource>

Posloupnost, která se má agregovat.

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

Akumulátorová funkce, která se použije u každého prvku.

Návraty

TSource

Konečná hodnota akumulátoru.

Výjimky

source nebo func je null.

source neobsahuje žádné prvky.

Příklady

Následující příklad kódu ukazuje, jak použít Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) k sestavení věty z pole řetězců.

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

Poznámky

Tato metoda má alespoň jeden parametr typu Expression<TDelegate> , jehož argument typu je jedním z Func<T,TResult> typů. Pro tyto parametry můžete předat výraz lambda, který se zkompiluje do Expression<TDelegate>.

Metoda Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) vygeneruje MethodCallExpression , který představuje volání Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) sebe sama jako konstruované obecné metody. Pak předá MethodCallExpressionExecute<TResult>(Expression) metodě parametru IQueryProvider reprezentované Provider vlastností source .

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, který představuje volání Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) , závisí na implementaci typu parametru source . Očekávané chování je, funcže se zadaná funkce použije na každou hodnotu ve zdrojové sekvenci a vrátí se kumulovaná hodnota. První hodnota v source se použije jako počáteční hodnota kumulované hodnoty, která odpovídá prvnímu parametru v funcsouboru .

Pro zjednodušení běžných agregačních operací zahrnuje sada standardních operátorů dotazu také dvě metody Count počítání a a LongCountčtyři metody číselné agregace, konkrétně Max, MinSum, a Average.

Platí pro