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 給以 Execute<TResult>(Expression) 參數的屬性所表示的方法 IQueryProvider Provider sourceIt 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>) 取決於參數類型的實作為 sourceThe 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. 預期的行為是它會傳回中的最後一個元素 sourceThe 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 給以 Execute<TResult>(Expression) 參數的屬性所表示的方法 IQueryProvider Provider sourceIt 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>>) 取決於參數類型的實作為 sourceThe 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 符合所指定之條件的最後一個元素 predicateThe expected behavior is that it returns the last element in source that satisfies the condition specified by predicate.

適用於