Queryable.ElementAt 方法

定義

多載

ElementAt<TSource>(IQueryable<TSource>, Index)

傳回位於序列中指定索引處的項目。

ElementAt<TSource>(IQueryable<TSource>, Int32)

傳回位於序列中指定索引處的項目。

ElementAt<TSource>(IQueryable<TSource>, Index)

傳回位於序列中指定索引處的項目。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource ElementAt(System::Linq::IQueryable<TSource> ^ source, Index index);
public static TSource ElementAt<TSource> (this System.Linq.IQueryable<TSource> source, Index index);
static member ElementAt : System.Linq.IQueryable<'Source> * Index -> 'Source
<Extension()>
Public Function ElementAt(Of TSource) (source As IQueryable(Of TSource), index As Index) As TSource

類型參數

TSource

source 項目的類型。

參數

source
IQueryable<TSource>

傳回項目的 IQueryable<T>

index
Index

要擷取之專案的索引,從開頭或結尾開始。

傳回

TSource

序列中位於指定位置的專案 source

例外狀況

sourcenull

index 超出序列界限 source

適用於

ElementAt<TSource>(IQueryable<TSource>, Int32)

傳回位於序列中指定索引處的項目。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource ElementAt(System::Linq::IQueryable<TSource> ^ source, int index);
public static TSource ElementAt<TSource> (this System.Linq.IQueryable<TSource> source, int index);
static member ElementAt : System.Linq.IQueryable<'Source> * int -> 'Source
<Extension()>
Public Function ElementAt(Of TSource) (source As IQueryable(Of TSource), index As Integer) As TSource

類型參數

TSource

source 項目的類型。

參數

source
IQueryable<TSource>

傳回項目的 IQueryable<T>

index
Int32

要擷取的項目之以零為起始索引。

傳回

TSource

位於 source 中指定之位置的項目。

例外狀況

sourcenull

index 小於零。

範例

下列程式碼範例示範如何使用 ElementAt<TSource>(IQueryable<TSource>, Int32) 傳回序列中特定位置的專案。

string[] names = { "Hartono, Tommy", "Adams, Terry",
                   "Andersen, Henriette Thaulow",
                   "Hedlund, Magnus", "Ito, Shu" };

Random random = new Random(DateTime.Now.Millisecond);

string name =
    names.AsQueryable().ElementAt(random.Next(0, names.Length));

Console.WriteLine("The name chosen at random is '{0}'.", name);

/*
    This code produces the following sample output.
    Yours may be different due to the use of Random.

    The name chosen at random is 'Ito, Shu'.
*/
Dim names() As String = {"Hartono, Tommy", "Adams, Terry", _
                   "Andersen, Henriette Thaulow", _
                   "Hedlund, Magnus", "Ito, Shu"}

Dim rand As New Random(DateTime.Now.Millisecond)

Dim name As String = _
    names.AsQueryable().ElementAt(rand.Next(0, names.Length))

MsgBox(String.Format("The name chosen at random is '{0}'.", name))

' This code produces the following sample output.
' Yours may be different due to the use of Random.
'
' The name chosen at random is 'Ito, Shu'.

備註

方法 ElementAt<TSource>(IQueryable<TSource>, Int32) 會產生 , MethodCallExpression 表示呼叫 ElementAt<TSource>(IQueryable<TSource>, Int32) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpression Execute<TResult>(Expression) 參數之 屬性所 Provider 表示的 方法 IQueryProvider source

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

適用於