Queryable.Max 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) |
对泛型 IQueryable<T> 的每个元素调用投影函数,并返回最大结果值。 |
Max<TSource>(IQueryable<TSource>) |
返回泛型 IQueryable<T> 中的最大值。 |
Max<TSource>(IQueryable<TSource>, IComparer<TSource>) |
返回泛型 IQueryable<T> 中的最大值。 |
Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>)
对泛型 IQueryable<T> 的每个元素调用投影函数,并返回最大结果值。
public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
static TResult Max(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TResult> ^> ^ selector);
public static TResult Max<TSource,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
public static TResult? Max<TSource,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
static member Max : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Result>> -> 'Result
<Extension()>
Public Function Max(Of TSource, TResult) (source As IQueryable(Of TSource), selector As Expression(Of Func(Of TSource, TResult))) As TResult
类型参数
- TSource
source
的元素类型。
- TResult
由 selector
表示的函数返回的值类型。
参数
- source
- IQueryable<TSource>
要确定最大值的值序列。
- selector
- Expression<Func<TSource,TResult>>
要应用于每个元素的投影函数。
返回
- TResult
序列中的最大值。
例外
source
或 selector
为 null
。
source
中不包含任何元素。
示例
下面的代码示例演示如何用于 Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 确定一系列投影值中的最大值。
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
public static void MaxEx2()
{
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
// Add Pet.Age to the length of Pet.Name
// to determine the "maximum" Pet object in the array.
int max =
pets.AsQueryable().Max(pet => pet.Age + pet.Name.Length);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
Structure Pet
Public Name As String
Public Age As Integer
End Structure
Shared Sub MaxEx2()
Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8}, _
New Pet With {.Name = "Boots", .Age = 4}, _
New Pet With {.Name = "Whiskers", .Age = 1}}
' Add Pet.Age to the length of Pet.Name
' to determine the "maximum" Pet object in the array.
Dim max As Integer = _
pets.AsQueryable().Max(Function(pet) pet.Age + pet.Name.Length)
MsgBox(String.Format("The maximum pet age plus name length is {0}.", max))
'This code produces the following output:
'The maximum pet age plus name length is 14.
注解
此方法至少有一个类型 Expression<TDelegate> 参数,其类型参数为类型之 Func<T,TResult> 一。 对于这些参数,可以传入 lambda 表达式,并将它编译为一个 Expression<TDelegate>。
该方法Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>)生成一个表示自己作为构造泛型方法调用Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>)自身的方法MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source
表示Provider的方法IQueryProvider。
执行表示调用 Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) 的表达式树导致的查询行为取决于参数类型的 source
实现。 预期行为是它在每个元素source
上调用selector
并返回最大值。
适用于
Max<TSource>(IQueryable<TSource>)
返回泛型 IQueryable<T> 中的最大值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Max(System::Linq::IQueryable<TSource> ^ source);
public static TSource Max<TSource> (this System.Linq.IQueryable<TSource> source);
public static TSource? Max<TSource> (this System.Linq.IQueryable<TSource> source);
static member Max : System.Linq.IQueryable<'Source> -> 'Source
<Extension()>
Public Function Max(Of TSource) (source As IQueryable(Of TSource)) As TSource
类型参数
- TSource
source
的元素类型。
参数
- source
- IQueryable<TSource>
要确定最大值的值序列。
返回
- TSource
序列中的最大值。
例外
source
为 null
。
source
中不包含任何元素。
示例
下面的代码示例演示如何用于 Max<TSource>(IQueryable<TSource>) 确定序列中的最大值。
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
long max = longs.AsQueryable().Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 4294967296.
*/
Dim longs As New List(Of Long)(New Long() {4294967296L, 466855135L, 81125L})
Dim max As Long = longs.AsQueryable().Max()
MsgBox(String.Format("The largest number is {0}.", max))
'This code produces the following output:
'The largest number is 4294967296.
注解
该方法Max<TSource>(IQueryable<TSource>)生成一个表示自己作为构造泛型方法调用Max<TSource>(IQueryable<TSource>)自身的方法MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source
表示Provider的方法IQueryProvider。
执行表示调用 Max<TSource>(IQueryable<TSource>) 的表达式树导致的查询行为取决于参数类型的 source
实现。 预期行为是它返回最大值。source
适用于
Max<TSource>(IQueryable<TSource>, IComparer<TSource>)
返回泛型 IQueryable<T> 中的最大值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Max(System::Linq::IQueryable<TSource> ^ source, System::Collections::Generic::IComparer<TSource> ^ comparer);
public static TSource? Max<TSource> (this System.Linq.IQueryable<TSource> source, System.Collections.Generic.IComparer<TSource>? comparer);
static member Max : System.Linq.IQueryable<'Source> * System.Collections.Generic.IComparer<'Source> -> 'Source
<Extension()>
Public Function Max(Of TSource) (source As IQueryable(Of TSource), comparer As IComparer(Of TSource)) As TSource
类型参数
- TSource
source
的元素类型。
参数
- source
- IQueryable<TSource>
要确定其最大值的值序列。
- comparer
- IComparer<TSource>
用于比较值的 IComparer<T>。
返回
- TSource
序列中的最大值。
例外
source
为 null
。