Queryable.Aggregate 方法

定义

重载

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

对序列应用累加器函数。 将指定的种子值用作累加器的初始值,并使用指定的函数选择结果值。

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

对序列应用累加器函数。 将指定的种子值用作累加器初始值。

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

对序列应用累加器函数。

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

对序列应用累加器函数。 将指定的种子值用作累加器的初始值,并使用指定的函数选择结果值。

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

类型参数

TSource

source 的元素类型。

TAccumulate

累加器值的类型。

TResult

结果值的类型。

参数

source
IQueryable<TSource>

要对其进行聚合的序列。

seed
TAccumulate

累加器的初始值。

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

要对每个元素调用的累加器函数。

selector
Expression<Func<TAccumulate,TResult>>

将累加器的最终值转换为结果值的函数。

返回

TResult

已转换的累加器最终值。

例外

sourcefuncselectornull

示例

下面的代码示例演示如何用于 Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) 应用累加器函数和结果选择器。

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.

注解

此方法至少有一个类型 Expression<TDelegate> 参数,其类型参数为类型之 Func<T,TResult> 一。 对于这些参数,可以传入 lambda 表达式,并将它编译为一个 Expression<TDelegate>

该方法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>>)自身的方法MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate, TResult>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是指定函数 func应用于源序列中的每个值,并返回累积值。 该 seed 参数用作累积值的种子值,对应于其中 func的第一个参数。 传递最终累积值以获取 selector 结果值。

为了简化常见的聚合操作,标准查询运算符集还包括两个计数方法,Count以及LongCount四个数值聚合方法,即MaxMinSumAverage

适用于

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

对序列应用累加器函数。 将指定的种子值用作累加器初始值。

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

类型参数

TSource

source 的元素类型。

TAccumulate

累加器值的类型。

参数

source
IQueryable<TSource>

要对其进行聚合的序列。

seed
TAccumulate

累加器的初始值。

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

要对每个元素调用的累加器函数。

返回

TAccumulate

累加器的最终值。

例外

sourcefuncnull

示例

下面的代码示例演示如何在向函数提供种子值时使用 Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) 累加器函数。

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

注解

此方法至少有一个类型 Expression<TDelegate> 参数,其类型参数为类型之 Func<T,TResult> 一。 对于这些参数,可以传入 lambda 表达式,并将它编译为一个 Expression<TDelegate>

该方法Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>)生成一个表示自己作为构造泛型方法调用Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>)自身的方法MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是指定函数 func应用于源序列中的每个值,并返回累积值。 该 seed 参数用作累积值的种子值,对应于其中 func的第一个参数。

为了简化常见的聚合操作,标准查询运算符集还包括两个计数方法,Count以及LongCount四个数值聚合方法,即MaxMinSumAverage

适用于

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

对序列应用累加器函数。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要对其进行聚合的序列。

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

要应用于每个元素的累加器函数。

返回

TSource

累加器的最终值。

例外

sourcefuncnull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于 Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) 从字符串数组生成句子。

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

注解

此方法至少有一个类型 Expression<TDelegate> 参数,其类型参数为类型之 Func<T,TResult> 一。 对于这些参数,可以传入 lambda 表达式,并将它编译为一个 Expression<TDelegate>

该方法Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>)生成一个表示自己作为构造泛型方法调用Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>)自身的方法MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是指定函数 func应用于源序列中的每个值,并返回累积值。 中的 source 第一个值用作累积值的种子值,该值对应于其中 func的第一个参数。

为了简化常见的聚合操作,标准查询运算符集还包括两个计数方法,Count以及LongCount四个数值聚合方法,即MaxMinSumAverage

适用于