Queryable.Average 方法

定义

计算数值序列的平均值。

重载

Average(IQueryable<Single>)

计算 Single 值序列的平均值。

Average(IQueryable<Nullable<Int64>>)

计算可以为 null 的 Int64 值序列的平均值。

Average(IQueryable<Nullable<Int32>>)

计算可以为 null 的 Int32 值序列的平均值。

Average(IQueryable<Nullable<Double>>)

计算可以为 null 的 Double 值序列的平均值。

Average(IQueryable<Nullable<Single>>)

计算可以为 null 的 Single 值序列的平均值。

Average(IQueryable<Int64>)

计算 Int64 值序列的平均值。

Average(IQueryable<Int32>)

计算 Int32 值序列的平均值。

Average(IQueryable<Double>)

计算 Double 值序列的平均值。

Average(IQueryable<Decimal>)

计算 Decimal 值序列的平均值。

Average(IQueryable<Nullable<Decimal>>)

计算可以为 null 的 Decimal 值序列的平均值。

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>)

计算 Single 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>)

计算可以为 null 的 Single 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>)

计算可以为 null 的 Int64 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>)

计算可以为 null 的 Int32 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>)

计算可以为 null 的 Double 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>)

计算 Int64 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)

计算 Int32 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>)

计算 Double 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>)

计算 Decimal 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>)

计算可以为 null 的 Decimal 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

Average(IQueryable<Single>)

计算 Single 值序列的平均值。

public:
[System::Runtime::CompilerServices::Extension]
 static float Average(System::Linq::IQueryable<float> ^ source);
public static float Average (this System.Linq.IQueryable<float> source);
static member Average : System.Linq.IQueryable<single> -> single
<Extension()>
Public Function Average (source As IQueryable(Of Single)) As Single

参数

source
IQueryable<Single>

要计算其平均值的 Single 值序列。

返回

Single

值序列的平均值。

例外

sourcenull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于 Average(IQueryable<Int32>) 计算值序列的平均值。

备注

此代码示例使用方法的重载,该重载不同于本文介绍的特定重载。 若要将此示例扩展到本文介绍的重载,请将源序列的元素替换为相应数字类型的元素。

List<int> grades = new List<int> { 78, 92, 100, 37, 81 };

double average = grades.AsQueryable().Average();

Console.WriteLine("The average grade is {0}.", average);

// This code produces the following output:
//
// The average grade is 77.6.
Dim grades As New List(Of Integer)(New Integer() {78, 92, 100, 37, 81})

Dim average As Double = grades.AsQueryable().Average()

MsgBox(String.Format("The average grade is {0}.", average))

' This code produces the following output:
'
' The average grade is 77.6.

注解

该方法Average(IQueryable<Single>)生成一个表示调用Average(IQueryable<Single>)本身的函数MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Average(IQueryable<Single>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是计算值平均值 source

适用于

Average(IQueryable<Nullable<Int64>>)

计算可以为 null 的 Int64 值序列的平均值。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Average(System::Linq::IQueryable<Nullable<long>> ^ source);
public static double? Average (this System.Linq.IQueryable<long?> source);
static member Average : System.Linq.IQueryable<Nullable<int64>> -> Nullable<double>
<Extension()>
Public Function Average (source As IQueryable(Of Nullable(Of Long))) As Nullable(Of Double)

参数

source
IQueryable<Nullable<Int64>>

要计算平均值的可以为 null 的 Int64 值序列。

返回

Nullable<Double>

如果源序列为空或仅包含 null 值,则为 null;否则为值序列的平均值。

例外

sourcenull

示例

下面的代码示例演示如何用于 Average(IQueryable<Nullable<Int64>>) 计算值序列的平均值。

long?[] longs = { null, 10007L, 37L, 399846234235L };

double? average = longs.AsQueryable().Average();

Console.WriteLine("The average is {0}.", average);

// This code produces the following output:
//
// The average is 133282081426.333.
Dim longs() As Nullable(Of Long) = {Nothing, 10007L, 37L, 399846234235L}

Dim average As Nullable(Of Double) = longs.AsQueryable().Average()

MsgBox(String.Format("The average is {0}.", average))

' This code produces the following output:
'
' The average is 133282081426.333.

注解

该方法Average(IQueryable<Nullable<Int64>>)生成一个表示调用Average(IQueryable<Nullable<Int64>>)本身的函数MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Average(IQueryable<Nullable<Int64>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是计算值平均值 source

适用于

Average(IQueryable<Nullable<Int32>>)

计算可以为 null 的 Int32 值序列的平均值。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Average(System::Linq::IQueryable<Nullable<int>> ^ source);
public static double? Average (this System.Linq.IQueryable<int?> source);
static member Average : System.Linq.IQueryable<Nullable<int>> -> Nullable<double>
<Extension()>
Public Function Average (source As IQueryable(Of Nullable(Of Integer))) As Nullable(Of Double)

参数

source
IQueryable<Nullable<Int32>>

要计算平均值的可以为 null 的 Int32 值序列。

返回

Nullable<Double>

如果源序列为空或仅包含 null 值,则为 null;否则为值序列的平均值。

例外

sourcenull

示例

下面的代码示例演示如何用于 Average(IQueryable<Nullable<Int64>>) 计算值序列的平均值。

备注

此代码示例使用方法的重载,该重载不同于本文介绍的特定重载。 若要将此示例扩展到本文介绍的重载,请将源序列的元素替换为相应数字类型的元素。

long?[] longs = { null, 10007L, 37L, 399846234235L };

double? average = longs.AsQueryable().Average();

Console.WriteLine("The average is {0}.", average);

// This code produces the following output:
//
// The average is 133282081426.333.
Dim longs() As Nullable(Of Long) = {Nothing, 10007L, 37L, 399846234235L}

Dim average As Nullable(Of Double) = longs.AsQueryable().Average()

MsgBox(String.Format("The average is {0}.", average))

' This code produces the following output:
'
' The average is 133282081426.333.

注解

该方法Average(IQueryable<Nullable<Int32>>)生成一个表示调用Average(IQueryable<Nullable<Int32>>)本身的函数MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Average(IQueryable<Nullable<Int32>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是计算值平均值 source

适用于

Average(IQueryable<Nullable<Double>>)

计算可以为 null 的 Double 值序列的平均值。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Average(System::Linq::IQueryable<Nullable<double>> ^ source);
public static double? Average (this System.Linq.IQueryable<double?> source);
static member Average : System.Linq.IQueryable<Nullable<double>> -> Nullable<double>
<Extension()>
Public Function Average (source As IQueryable(Of Nullable(Of Double))) As Nullable(Of Double)

参数

source
IQueryable<Nullable<Double>>

要计算平均值的可以为 null 的 Double 值序列。

返回

Nullable<Double>

如果源序列为空或仅包含 null 值,则为 null;否则为值序列的平均值。

例外

sourcenull

示例

下面的代码示例演示如何用于 Average(IQueryable<Nullable<Int64>>) 计算值序列的平均值。

备注

此代码示例使用方法的重载,该重载不同于本文介绍的特定重载。 若要将此示例扩展到本文介绍的重载,请将源序列的元素替换为相应数字类型的元素。

long?[] longs = { null, 10007L, 37L, 399846234235L };

double? average = longs.AsQueryable().Average();

Console.WriteLine("The average is {0}.", average);

// This code produces the following output:
//
// The average is 133282081426.333.
Dim longs() As Nullable(Of Long) = {Nothing, 10007L, 37L, 399846234235L}

Dim average As Nullable(Of Double) = longs.AsQueryable().Average()

MsgBox(String.Format("The average is {0}.", average))

' This code produces the following output:
'
' The average is 133282081426.333.

注解

该方法Average(IQueryable<Nullable<Double>>)生成一个表示调用Average(IQueryable<Nullable<Double>>)本身的函数MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Average(IQueryable<Nullable<Double>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是计算值平均值 source

适用于

Average(IQueryable<Nullable<Single>>)

计算可以为 null 的 Single 值序列的平均值。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<float> Average(System::Linq::IQueryable<Nullable<float>> ^ source);
public static float? Average (this System.Linq.IQueryable<float?> source);
static member Average : System.Linq.IQueryable<Nullable<single>> -> Nullable<single>
<Extension()>
Public Function Average (source As IQueryable(Of Nullable(Of Single))) As Nullable(Of Single)

参数

source
IQueryable<Nullable<Single>>

要计算平均值的可以为 null 的 Single 值序列。

返回

Nullable<Single>

如果源序列为空或仅包含 null 值,则为 null;否则为值序列的平均值。

例外

sourcenull

示例

下面的代码示例演示如何用于 Average(IQueryable<Nullable<Int64>>) 计算值序列的平均值。

备注

此代码示例使用方法的重载,该重载不同于本文介绍的特定重载。 若要将此示例扩展到本文介绍的重载,请将源序列的元素替换为相应数字类型的元素。

long?[] longs = { null, 10007L, 37L, 399846234235L };

double? average = longs.AsQueryable().Average();

Console.WriteLine("The average is {0}.", average);

// This code produces the following output:
//
// The average is 133282081426.333.
Dim longs() As Nullable(Of Long) = {Nothing, 10007L, 37L, 399846234235L}

Dim average As Nullable(Of Double) = longs.AsQueryable().Average()

MsgBox(String.Format("The average is {0}.", average))

' This code produces the following output:
'
' The average is 133282081426.333.

注解

该方法Average(IQueryable<Nullable<Single>>)生成一个表示调用Average(IQueryable<Nullable<Single>>)本身的函数MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Average(IQueryable<Nullable<Single>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是计算值平均值 source

适用于

Average(IQueryable<Int64>)

计算 Int64 值序列的平均值。

public:
[System::Runtime::CompilerServices::Extension]
 static double Average(System::Linq::IQueryable<long> ^ source);
public static double Average (this System.Linq.IQueryable<long> source);
static member Average : System.Linq.IQueryable<int64> -> double
<Extension()>
Public Function Average (source As IQueryable(Of Long)) As Double

参数

source
IQueryable<Int64>

要计算其平均值的 Int64 值序列。

返回

Double

值序列的平均值。

例外

sourcenull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于 Average(IQueryable<Int32>) 计算值序列的平均值。

备注

此代码示例使用方法的重载,该重载不同于本文介绍的特定重载。 若要将此示例扩展到本文介绍的重载,请将源序列的元素替换为相应数字类型的元素。

List<int> grades = new List<int> { 78, 92, 100, 37, 81 };

double average = grades.AsQueryable().Average();

Console.WriteLine("The average grade is {0}.", average);

// This code produces the following output:
//
// The average grade is 77.6.
Dim grades As New List(Of Integer)(New Integer() {78, 92, 100, 37, 81})

Dim average As Double = grades.AsQueryable().Average()

MsgBox(String.Format("The average grade is {0}.", average))

' This code produces the following output:
'
' The average grade is 77.6.

注解

该方法Average(IQueryable<Int64>)生成一个表示调用Average(IQueryable<Int64>)本身的函数MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Average(IQueryable<Int64>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是计算值平均值 source

适用于

Average(IQueryable<Int32>)

计算 Int32 值序列的平均值。

public:
[System::Runtime::CompilerServices::Extension]
 static double Average(System::Linq::IQueryable<int> ^ source);
public static double Average (this System.Linq.IQueryable<int> source);
static member Average : System.Linq.IQueryable<int> -> double
<Extension()>
Public Function Average (source As IQueryable(Of Integer)) As Double

参数

source
IQueryable<Int32>

要计算其平均值的 Int32 值序列。

返回

Double

值序列的平均值。

例外

sourcenull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于 Average(IQueryable<Int32>) 计算值序列的平均值。

List<int> grades = new List<int> { 78, 92, 100, 37, 81 };

double average = grades.AsQueryable().Average();

Console.WriteLine("The average grade is {0}.", average);

// This code produces the following output:
//
// The average grade is 77.6.
Dim grades As New List(Of Integer)(New Integer() {78, 92, 100, 37, 81})

Dim average As Double = grades.AsQueryable().Average()

MsgBox(String.Format("The average grade is {0}.", average))

' This code produces the following output:
'
' The average grade is 77.6.

注解

该方法Average(IQueryable<Int32>)生成一个表示调用Average(IQueryable<Int32>)本身的函数MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Average(IQueryable<Int32>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是计算值平均值 source

适用于

Average(IQueryable<Double>)

计算 Double 值序列的平均值。

public:
[System::Runtime::CompilerServices::Extension]
 static double Average(System::Linq::IQueryable<double> ^ source);
public static double Average (this System.Linq.IQueryable<double> source);
static member Average : System.Linq.IQueryable<double> -> double
<Extension()>
Public Function Average (source As IQueryable(Of Double)) As Double

参数

source
IQueryable<Double>

要计算其平均值的 Double 值序列。

返回

Double

值序列的平均值。

例外

sourcenull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于 Average(IQueryable<Int32>) 计算值序列的平均值。

备注

此代码示例使用方法的重载,该重载不同于本文介绍的特定重载。 若要将此示例扩展到本文介绍的重载,请将源序列的元素替换为相应数字类型的元素。

List<int> grades = new List<int> { 78, 92, 100, 37, 81 };

double average = grades.AsQueryable().Average();

Console.WriteLine("The average grade is {0}.", average);

// This code produces the following output:
//
// The average grade is 77.6.
Dim grades As New List(Of Integer)(New Integer() {78, 92, 100, 37, 81})

Dim average As Double = grades.AsQueryable().Average()

MsgBox(String.Format("The average grade is {0}.", average))

' This code produces the following output:
'
' The average grade is 77.6.

注解

该方法Average(IQueryable<Double>)生成一个表示调用Average(IQueryable<Double>)本身的函数MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Average(IQueryable<Double>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是计算值平均值 source

适用于

Average(IQueryable<Decimal>)

计算 Decimal 值序列的平均值。

public:
[System::Runtime::CompilerServices::Extension]
 static System::Decimal Average(System::Linq::IQueryable<System::Decimal> ^ source);
public static decimal Average (this System.Linq.IQueryable<decimal> source);
static member Average : System.Linq.IQueryable<decimal> -> decimal
<Extension()>
Public Function Average (source As IQueryable(Of Decimal)) As Decimal

参数

source
IQueryable<Decimal>

要计算其平均值的 Decimal 值序列。

返回

Decimal

值序列的平均值。

例外

sourcenull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于 Average(IQueryable<Int32>) 计算值序列的平均值。

备注

此代码示例使用方法的重载,该重载不同于本文介绍的特定重载。 若要将此示例扩展到本文介绍的重载,请将源序列的元素替换为相应数字类型的元素。

List<int> grades = new List<int> { 78, 92, 100, 37, 81 };

double average = grades.AsQueryable().Average();

Console.WriteLine("The average grade is {0}.", average);

// This code produces the following output:
//
// The average grade is 77.6.
Dim grades As New List(Of Integer)(New Integer() {78, 92, 100, 37, 81})

Dim average As Double = grades.AsQueryable().Average()

MsgBox(String.Format("The average grade is {0}.", average))

' This code produces the following output:
'
' The average grade is 77.6.

注解

该方法Average(IQueryable<Decimal>)生成一个表示调用Average(IQueryable<Decimal>)本身的函数MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Average(IQueryable<Decimal>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是计算值平均值 source

适用于

Average(IQueryable<Nullable<Decimal>>)

计算可以为 null 的 Decimal 值序列的平均值。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<System::Decimal> Average(System::Linq::IQueryable<Nullable<System::Decimal>> ^ source);
public static decimal? Average (this System.Linq.IQueryable<decimal?> source);
static member Average : System.Linq.IQueryable<Nullable<decimal>> -> Nullable<decimal>
<Extension()>
Public Function Average (source As IQueryable(Of Nullable(Of Decimal))) As Nullable(Of Decimal)

参数

source
IQueryable<Nullable<Decimal>>

要计算平均值的可以为 null 的 Decimal 值序列。

返回

Nullable<Decimal>

如果源序列为空或仅包含 null 值,则为 null;否则为值序列的平均值。

例外

sourcenull

示例

下面的代码示例演示如何用于 Average(IQueryable<Nullable<Int64>>) 计算值序列的平均值。

备注

此代码示例使用方法的重载,该重载不同于本文介绍的特定重载。 若要将此示例扩展到本文介绍的重载,请将源序列的元素替换为相应数字类型的元素。

long?[] longs = { null, 10007L, 37L, 399846234235L };

double? average = longs.AsQueryable().Average();

Console.WriteLine("The average is {0}.", average);

// This code produces the following output:
//
// The average is 133282081426.333.
Dim longs() As Nullable(Of Long) = {Nothing, 10007L, 37L, 399846234235L}

Dim average As Nullable(Of Double) = longs.AsQueryable().Average()

MsgBox(String.Format("The average is {0}.", average))

' This code produces the following output:
'
' The average is 133282081426.333.

注解

该方法Average(IQueryable<Nullable<Decimal>>)生成一个表示调用Average(IQueryable<Nullable<Decimal>>)本身的函数MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Average(IQueryable<Nullable<Decimal>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是计算值平均值 source

适用于

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>)

计算 Single 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要计算其平均值的值序列。

selector
Expression<Func<TSource,Single>>

要应用于每个元素的投影函数。

返回

Single

值序列的平均值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)计算类型String值序列中的平均String长度。

备注

此代码示例使用 方法的重载,该重载不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改函数 selector 的主体。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

// Determine the average string length in the array.
double average = fruits.AsQueryable().Average(s => s.Length);

Console.WriteLine("The average string length is {0}.", average);

// This code produces the following output:
//
// The average string length is 6.5.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Determine the average string length in the array.
Dim average As Double = fruits.AsQueryable().Average(Function(s) s.Length)

MsgBox(String.Format("The average string length is {0}.", average))

' This code produces the following output:
'
' The average string length is 6.5.

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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

执行表示调用 Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它在调用selector每个值source后计算值平均值。

适用于

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>)

计算可以为 null 的 Single 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要计算其平均值的值序列。

selector
Expression<Func<TSource,Nullable<Single>>>

要应用于每个元素的投影函数。

返回

Nullable<Single>

值序列的平均值;如果 source 序列为空或仅包含 null 值,则为 null

例外

sourceselectornull

示例

下面的代码示例演示如何用于Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)计算类型String值序列中的平均String长度。

备注

此代码示例使用 方法的重载,该重载不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改函数 selector 的主体。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

// Determine the average string length in the array.
double average = fruits.AsQueryable().Average(s => s.Length);

Console.WriteLine("The average string length is {0}.", average);

// This code produces the following output:
//
// The average string length is 6.5.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Determine the average string length in the array.
Dim average As Double = fruits.AsQueryable().Average(Function(s) s.Length)

MsgBox(String.Format("The average string length is {0}.", average))

' This code produces the following output:
'
' The average string length is 6.5.

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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

执行表示调用 Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它在调用selector每个值source后计算值平均值。

适用于

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>)

计算可以为 null 的 Int64 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Average(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, Nullable<long>> ^> ^ selector);
public static double? Average<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,long?>> selector);
static member Average : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, Nullable<int64>>> -> Nullable<double>
<Extension()>
Public Function Average(Of TSource) (source As IQueryable(Of TSource), selector As Expression(Of Func(Of TSource, Nullable(Of Long)))) As Nullable(Of Double)

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要计算其平均值的值序列。

selector
Expression<Func<TSource,Nullable<Int64>>>

要应用于每个元素的投影函数。

返回

Nullable<Double>

值序列的平均值;如果 source 序列为空或仅包含 null 值,则为 null

例外

sourceselectornull

示例

下面的代码示例演示如何用于Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)计算类型String值序列中的平均String长度。

备注

此代码示例使用 方法的重载,该重载不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改函数 selector 的主体。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

// Determine the average string length in the array.
double average = fruits.AsQueryable().Average(s => s.Length);

Console.WriteLine("The average string length is {0}.", average);

// This code produces the following output:
//
// The average string length is 6.5.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Determine the average string length in the array.
Dim average As Double = fruits.AsQueryable().Average(Function(s) s.Length)

MsgBox(String.Format("The average string length is {0}.", average))

' This code produces the following output:
'
' The average string length is 6.5.

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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

执行表示调用 Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它在调用selector每个值source后计算值平均值。

适用于

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>)

计算可以为 null 的 Int32 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Average(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, Nullable<int>> ^> ^ selector);
public static double? Average<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int?>> selector);
static member Average : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, Nullable<int>>> -> Nullable<double>
<Extension()>
Public Function Average(Of TSource) (source As IQueryable(Of TSource), selector As Expression(Of Func(Of TSource, Nullable(Of Integer)))) As Nullable(Of Double)

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要计算其平均值的值序列。

selector
Expression<Func<TSource,Nullable<Int32>>>

要应用于每个元素的投影函数。

返回

Nullable<Double>

值序列的平均值;如果 source 序列为空或仅包含 null 值,则为 null

例外

sourceselectornull

示例

下面的代码示例演示如何用于Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)计算类型String值序列中的平均String长度。

备注

此代码示例使用 方法的重载,该重载不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改函数 selector 的主体。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

// Determine the average string length in the array.
double average = fruits.AsQueryable().Average(s => s.Length);

Console.WriteLine("The average string length is {0}.", average);

// This code produces the following output:
//
// The average string length is 6.5.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Determine the average string length in the array.
Dim average As Double = fruits.AsQueryable().Average(Function(s) s.Length)

MsgBox(String.Format("The average string length is {0}.", average))

' This code produces the following output:
'
' The average string length is 6.5.

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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

执行表示调用 Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它在调用selector每个值source后计算值平均值。

适用于

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>)

计算可以为 null 的 Double 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要计算其平均值的值序列。

selector
Expression<Func<TSource,Nullable<Double>>>

要应用于每个元素的投影函数。

返回

Nullable<Double>

值序列的平均值;如果 source 序列为空或仅包含 null 值,则为 null

例外

sourceselectornull

示例

下面的代码示例演示如何用于Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)计算类型String值序列中的平均String长度。

备注

此代码示例使用 方法的重载,该重载不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改函数 selector 的主体。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

// Determine the average string length in the array.
double average = fruits.AsQueryable().Average(s => s.Length);

Console.WriteLine("The average string length is {0}.", average);

// This code produces the following output:
//
// The average string length is 6.5.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Determine the average string length in the array.
Dim average As Double = fruits.AsQueryable().Average(Function(s) s.Length)

MsgBox(String.Format("The average string length is {0}.", average))

' This code produces the following output:
'
' The average string length is 6.5.

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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

执行表示调用 Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它在调用selector每个值source后计算值平均值。

适用于

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>)

计算 Int64 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要计算其平均值的值序列。

selector
Expression<Func<TSource,Int64>>

要应用于每个元素的投影函数。

返回

Double

值序列的平均值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)计算类型String值序列中的平均String长度。

备注

此代码示例使用 方法的重载,该重载不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改函数 selector 的主体。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

// Determine the average string length in the array.
double average = fruits.AsQueryable().Average(s => s.Length);

Console.WriteLine("The average string length is {0}.", average);

// This code produces the following output:
//
// The average string length is 6.5.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Determine the average string length in the array.
Dim average As Double = fruits.AsQueryable().Average(Function(s) s.Length)

MsgBox(String.Format("The average string length is {0}.", average))

' This code produces the following output:
'
' The average string length is 6.5.

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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

执行表示调用 Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它在调用selector每个值source后计算值平均值。

适用于

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)

计算 Int32 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要计算其平均值的值序列。

selector
Expression<Func<TSource,Int32>>

要应用于每个元素的投影函数。

返回

Double

值序列的平均值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)计算类型String值序列中的平均String长度。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

// Determine the average string length in the array.
double average = fruits.AsQueryable().Average(s => s.Length);

Console.WriteLine("The average string length is {0}.", average);

// This code produces the following output:
//
// The average string length is 6.5.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Determine the average string length in the array.
Dim average As Double = fruits.AsQueryable().Average(Function(s) s.Length)

MsgBox(String.Format("The average string length is {0}.", average))

' This code produces the following output:
'
' The average string length is 6.5.

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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

执行表示调用 Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它在调用selector每个值source后计算值平均值。

适用于

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>)

计算 Double 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要计算其平均值的值序列。

selector
Expression<Func<TSource,Double>>

要应用于每个元素的投影函数。

返回

Double

值序列的平均值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)计算类型String值序列中的平均String长度。

备注

此代码示例使用 方法的重载,该重载不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改函数 selector 的主体。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

// Determine the average string length in the array.
double average = fruits.AsQueryable().Average(s => s.Length);

Console.WriteLine("The average string length is {0}.", average);

// This code produces the following output:
//
// The average string length is 6.5.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Determine the average string length in the array.
Dim average As Double = fruits.AsQueryable().Average(Function(s) s.Length)

MsgBox(String.Format("The average string length is {0}.", average))

' This code produces the following output:
'
' The average string length is 6.5.

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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

执行表示调用 Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它在调用selector每个值source后计算值平均值。

适用于

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>)

计算 Decimal 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

用于计算平均值的值序列。

selector
Expression<Func<TSource,Decimal>>

要应用于每个元素的投影函数。

返回

Decimal

值序列的平均值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何用于Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)计算类型String值序列中的平均String长度。

备注

此代码示例使用 方法的重载,该重载不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改函数 selector 的主体。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

// Determine the average string length in the array.
double average = fruits.AsQueryable().Average(s => s.Length);

Console.WriteLine("The average string length is {0}.", average);

// This code produces the following output:
//
// The average string length is 6.5.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Determine the average string length in the array.
Dim average As Double = fruits.AsQueryable().Average(Function(s) s.Length)

MsgBox(String.Format("The average string length is {0}.", average))

' This code produces the following output:
'
' The average string length is 6.5.

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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

执行表示调用 Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它在调用selector每个值source后计算值平均值。

适用于

Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>)

计算可以为 null 的 Decimal 值序列的平均值,该序列是通过对输入序列中的每个元素调用投影函数而获得的。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要计算其平均值的值序列。

selector
Expression<Func<TSource,Nullable<Decimal>>>

要应用于每个元素的投影函数。

返回

Nullable<Decimal>

值序列的平均值;如果 source 序列为空或仅包含 null 值,则为 null

例外

sourceselectornull

示例

下面的代码示例演示如何用于Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)计算类型String值序列中的平均String长度。

备注

此代码示例使用 方法的重载,该重载不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改函数 selector 的主体。

string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

// Determine the average string length in the array.
double average = fruits.AsQueryable().Average(s => s.Length);

Console.WriteLine("The average string length is {0}.", average);

// This code produces the following output:
//
// The average string length is 6.5.
Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Determine the average string length in the array.
Dim average As Double = fruits.AsQueryable().Average(Function(s) s.Length)

MsgBox(String.Format("The average string length is {0}.", average))

' This code produces the following output:
'
' The average string length is 6.5.

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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

执行表示调用 Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它在调用selector每个值source后计算值平均值。

适用于