Queryable.Last 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回序列中的最后一个元素。
重载
Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) |
返回序列中满足指定条件的最后一个元素。 |
Last<TSource>(IQueryable<TSource>) |
返回序列中的最后一个元素。 |
Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
返回序列中满足指定条件的最后一个元素。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Last(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static TSource Last<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member Last : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
<Extension()>
Public Function Last(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As TSource
类型参数
- TSource
source
的元素类型。
参数
- source
- IQueryable<TSource>
要从中返回元素的 IQueryable<T>。
- predicate
- Expression<Func<TSource,Boolean>>
用于测试每个元素是否满足条件的函数。
返回
- TSource
source
中的最后一个元素,它通过了由 predicate
指定的测试。
例外
source
或 predicate
为 null
。
示例
下面的代码示例演示如何用于 Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 返回满足条件的数组的最后一个元素。
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
83, 23, 87, 67, 12, 19 };
// Get the last number in the array that is greater than 80.
int last = numbers.AsQueryable().Last(num => num > 80);
Console.WriteLine(last);
/*
This code produces the following output:
87
*/
Dim numbers() As Integer = {9, 34, 65, 92, 87, 435, 3, 54, _
83, 23, 87, 67, 12, 19}
' Get the last number in the array that is greater than 80.
Dim last As Integer = numbers.AsQueryable().Last(Function(num) num > 80)
MsgBox(last)
' This code produces the following output:
' 87
注解
此方法至少有一个类型 Expression<TDelegate> 参数,其类型参数为类型之 Func<T,TResult> 一。 对于这些参数,可以传入 lambda 表达式,并将它编译为一个 Expression<TDelegate>。
该方法Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)生成一个表示自己作为构造泛型方法调用Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)自身的方法MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source
表示Provider的方法IQueryProvider。
执行表示调用 Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表达式树导致的查询行为取决于参数类型的 source
实现。 预期行为是,它返回满足由predicate
指定条件的最后source
一个元素。
适用于
Last<TSource>(IQueryable<TSource>)
返回序列中的最后一个元素。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Last(System::Linq::IQueryable<TSource> ^ source);
public static TSource Last<TSource> (this System.Linq.IQueryable<TSource> source);
static member Last : System.Linq.IQueryable<'Source> -> 'Source
<Extension()>
Public Function Last(Of TSource) (source As IQueryable(Of TSource)) As TSource
类型参数
- TSource
source
的元素类型。
参数
- source
- IQueryable<TSource>
要返回其最后一个元素的 IQueryable<T>。
返回
- TSource
位于 source
中最后位置处的值。
例外
source
为 null
。
源序列为空。
示例
下面的代码示例演示如何用于 Last<TSource>(IQueryable<TSource>) 返回数组的最后一个元素。
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
83, 23, 87, 67, 12, 19 };
int last = numbers.AsQueryable().Last();
Console.WriteLine(last);
/*
This code produces the following output:
19
*/
Dim numbers() As Integer = {9, 34, 65, 92, 87, 435, 3, 54, _
83, 23, 87, 67, 12, 19}
Dim last As Integer = numbers.AsQueryable().Last()
MsgBox(last)
' This code produces the following output:
' 19
注解
该方法Last<TSource>(IQueryable<TSource>)生成一个表示自己作为构造泛型方法调用Last<TSource>(IQueryable<TSource>)自身的方法MethodCallExpression。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source
表示Provider的方法IQueryProvider。
执行表示调用 Last<TSource>(IQueryable<TSource>) 的表达式树导致的查询行为取决于参数类型的 source
实现。 预期行为是它返回最后一个元素。source