Queryable.Last 方法

定義

傳回序列中的最後一個項目。

多載

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

傳回序列中符合指定之條件的最後一個元素。

Last<TSource>(IQueryable<TSource>)

傳回序列中的最後一個項目。

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
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 表示的 方法 IQueryProvidersource

執行表示呼叫 Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的運算式樹狀結構所產生的查詢行為,取決於參數類型的實作 source 。 預期的行為是它會傳回 中 source 符合 所 predicate 指定條件的最後一個專案。

適用於

Last<TSource>(IQueryable<TSource>)

來源:
Queryable.cs
來源:
Queryable.cs
來源:
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 表示的 方法 IQueryProvidersource

執行表示呼叫 Last<TSource>(IQueryable<TSource>) 的運算式樹狀結構所產生的查詢行為,取決於參數類型的實作 source 。 預期的行為是它會傳回 中的 source 最後一個專案。

適用於