Queryable.Last 方法

定义

返回序列中的最后一个元素。

重载

Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

返回序列中满足指定条件的最后一个元素。

Last<TSource>(IQueryable<TSource>)

返回序列中的最后一个元素。

Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

返回序列中满足指定条件的最后一个元素。

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 指定的测试。

例外

sourcepredicatenull

元素均不满足 predicate 中的条件。

- 或 -

源序列为空。

示例

下面的代码示例演示如何使用 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>>) 生成一个 , MethodCallExpression 表示将调用 Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 自身作为构造的泛型方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表达式树而发生的查询行为取决于参数类型的 source 实现。 预期的行为是,它返回 中 source 满足 指定 predicate条件的最后一个元素。

适用于

Last<TSource>(IQueryable<TSource>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

返回序列中的最后一个元素。

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 中最后位置处的值。

例外

sourcenull

源序列为空。

示例

下面的代码示例演示如何使用 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>) 生成一个 , MethodCallExpression 表示将调用 Last<TSource>(IQueryable<TSource>) 自身作为构造的泛型方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 Last<TSource>(IQueryable<TSource>) 的表达式树而发生的查询行为取决于参数类型的 source 实现。 预期的行为是它返回 中的 source最后一个元素。

适用于