Queryable.ElementAt Metoda

Definicja

Przeciążenia

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

Zwraca element w określonym indeksie w sekwencji.

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

Zwraca element w określonym indeksie w sekwencji.

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

Zwraca element w określonym indeksie w sekwencji.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element IQueryable<T> do zwrócenia elementu z.

index
Index

Indeks elementu do pobrania, który jest od początku lub końca.

Zwraca

TSource

Element w określonej pozycji w source sekwencji.

Wyjątki

source to null.

index znajduje się poza granicami source sekwencji.

Dotyczy

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

Zwraca element w określonym indeksie w sekwencji.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element IQueryable<T> do zwrócenia elementu z.

index
Int32

Indeks zerowy elementu do pobrania.

Zwraca

TSource

Element znajdujący się na określonej pozycji w sourceelem.

Wyjątki

source to null.

Parametr index ma wartość niższą niż zero.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć polecenia ElementAt<TSource>(IQueryable<TSource>, Int32) , aby zwrócić element w określonej pozycji w sekwencji.

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'.

Uwagi

Metoda ElementAt<TSource>(IQueryable<TSource>, Int32) generuje obiekt MethodCallExpression , który reprezentuje wywołanie ElementAt<TSource>(IQueryable<TSource>, Int32) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do Execute<TResult>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie ElementAt<TSource>(IQueryable<TSource>, Int32) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że zwraca element na pozycji w sourceelemencie index .

Dotyczy