Queryable.Last 方法

定义

返回序列中的最后一个元素。Returns the last element in a sequence.

重载

Last<TSource>(IQueryable<TSource>)

返回序列中的最后一个元素。Returns the last element in a sequence.

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

返回序列中满足指定条件的最后一个元素。Returns the last element of a sequence that satisfies a specified condition.

Last<TSource>(IQueryable<TSource>)

返回序列中的最后一个元素。Returns the last element in a sequence.

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 的元素类型。The type of the elements of source.

参数

source
IQueryable<TSource>

要返回其最后一个元素的 IQueryable<T>An IQueryable<T> to return the last element of.

返回

TSource

位于 source 中最后位置处的值。The value at the last position in source.

异常

sourcenullsource is null.

源序列为空。The source sequence is empty.

示例

下面的代码示例演示如何使用 Last<TSource>(IQueryable<TSource>) 返回数组的最后一个元素。The following code example demonstrates how to use Last<TSource>(IQueryable<TSource>) to return the last element of an array.

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>) 本身。The Last<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling Last<TSource>(IQueryable<TSource>) itself as a constructed generic method. 然后,它将 MethodCallExpression 传递到 IQueryProviderExecute<TResult>(Expression) 方法(由 source 参数的 Provider 属性表示)。It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

因执行表示调用 Last<TSource>(IQueryable<TSource>) 的表达式树而发生的查询行为取决于 source 参数类型的实现。The query behavior that occurs as a result of executing an expression tree that represents calling Last<TSource>(IQueryable<TSource>) depends on the implementation of the type of the source parameter. 预期的行为是返回 source中的最后一个元素。The expected behavior is that it returns the last element in source.

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

返回序列中满足指定条件的最后一个元素。Returns the last element of a sequence that satisfies a specified condition.

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 的元素类型。The type of the elements of source.

参数

source
IQueryable<TSource>

要从中返回元素的 IQueryable<T>An IQueryable<T> to return an element from.

predicate
Expression<Func<TSource,Boolean>>

用于测试每个元素是否满足条件的函数。A function to test each element for a condition.

返回

TSource

source 中的最后一个元素,它通过了由 predicate 指定的测试。The last element in source that passes the test specified by predicate.

异常

sourcepredicatenullsource or predicate is null.

元素均不满足 predicate 中的条件。No element satisfies the condition in predicate.

- 或 --or- 源序列为空。The source sequence is empty.

示例

下面的代码示例演示如何使用 Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 返回数组中满足条件的最后一个元素。The following code example demonstrates how to use Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) to return the last element of an array that satisfies a condition.

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> 类型之一。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. 对于这些参数,可以传入 lambda 表达式,并将其编译为 Expression<TDelegate>For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 方法生成一个 MethodCallExpression,它表示作为构造的泛型方法调用 Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 本身。The Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a MethodCallExpression that represents calling Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) itself as a constructed generic method. 然后,它将 MethodCallExpression 传递到 IQueryProviderExecute<TResult>(Expression) 方法(由 source 参数的 Provider 属性表示)。It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

因执行表示调用 Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表达式树而发生的查询行为取决于 source 参数类型的实现。The query behavior that occurs as a result of executing an expression tree that represents calling Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the source parameter. 预期的行为是返回 source 中满足 predicate指定的条件的最后一个元素。The expected behavior is that it returns the last element in source that satisfies the condition specified by predicate.

适用于