Queryable.ElementAt Metoda

Definice

Přetížení

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

Vrátí prvek v zadaném indexu v posloupnosti.

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

Vrátí prvek v zadaném indexu v posloupnosti.

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

Vrátí prvek v zadaném indexu v posloupnosti.

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 prvků .source

Parametry

source
IQueryable<TSource>

Návrat IQueryable<T> prvku z.

index
Index

Index prvku, který se má načíst, což je buď od začátku, nebo konce.

Návraty

TSource

Prvek na zadané pozici v source sekvenci.

Výjimky

source je null.

index je mimo hranice source sekvence.

Platí pro

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

Vrátí prvek v zadaném indexu v posloupnosti.

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 prvků .source

Parametry

source
IQueryable<TSource>

Návrat IQueryable<T> prvku z.

index
Int32

Index založený na nule elementu, který se má načíst.

Návraty

TSource

Prvek na zadané pozici v source.

Výjimky

source je null.

Hodnota index je menší než nula.

Příklady

Následující příklad kódu ukazuje, jak použít ElementAt<TSource>(IQueryable<TSource>, Int32) k vrácení elementu na konkrétní pozici v posloupnosti.

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

Poznámky

Metoda ElementAt<TSource>(IQueryable<TSource>, Int32) vygeneruje MethodCallExpression volání ElementAt<TSource>(IQueryable<TSource>, Int32) jako vytvořenou obecnou metodu. Pak předá MethodCallExpression metodu IQueryProvider Execute<TResult>(Expression) reprezentované Provider vlastností parametrusource.

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, který představuje volání ElementAt<TSource>(IQueryable<TSource>, Int32) , závisí na implementaci typu parametru source . Očekávané chování spočívá v tom, že vrátí položku na pozici index v source.

Platí pro