Queryable.ElementAt<TSource>(IQueryable<TSource>, Int32) 方法

定義

傳回位於序列中指定索引處的項目。Returns the element at a specified index in a sequence.

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 項目的類型。The type of the elements of source.

參數

source
IQueryable<TSource>

傳回項目的 IQueryable<T>An IQueryable<T> to return an element from.

index
Int32

要擷取的項目之以零為起始索引。The zero-based index of the element to retrieve.

傳回

TSource

位於 source 中指定之位置的項目。The element at the specified position in source.

例外狀況

sourcenullsource is null.

index 小於零。index is less than zero.

範例

下列程式碼範例示範如何使用傳回 ElementAt<TSource>(IQueryable<TSource>, Int32) 序列中特定位置的元素。The following code example demonstrates how to use ElementAt<TSource>(IQueryable<TSource>, Int32) to return an element at a specific position in a sequence.

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) 本身做為已建立的泛型方法。The ElementAt<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling ElementAt<TSource>(IQueryable<TSource>, Int32) 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.

執行表示呼叫的運算式樹狀架構所產生的查詢行為, ElementAt<TSource>(IQueryable<TSource>, Int32) 取決於參數類型的實作為 sourceThe query behavior that occurs as a result of executing an expression tree that represents calling ElementAt<TSource>(IQueryable<TSource>, Int32) depends on the implementation of the type of the source parameter. 預期的行為是它會在中的位置傳回 index 專案 sourceThe expected behavior is that it returns the item at position index in source.

適用於