Queryable.ElementAt<TSource>(IQueryable<TSource>, Int32) Metódus

Definíció

Egy adott index elemet ad vissza egy sorozatban.

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

Típusparaméterek

TSource

A . elemeinek sourcetípusa.

Paraméterek

source
IQueryable<TSource>

Egy IQueryable<T> elem visszaadásához.

index
Int32

A lekérni kívánt elem nulla alapú indexe.

Válaszok

TSource

A megadott pozícióban lévő elem a következőben source: .

Kivételek

source az null.

index kisebb, mint nulla.

Példák

Az alábbi példakód bemutatja, hogyan lehet ElementAt<TSource>(IQueryable<TSource>, Int32) visszaadni egy elemet egy adott pozícióban egy sorozatban.

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

Megjegyzések

A ElementAt<TSource>(IQueryable<TSource>, Int32) metódus létrehoz egy olyan metódust MethodCallExpression , amely önmagát generált általános metódusként jeleníti ElementAt<TSource>(IQueryable<TSource>, Int32) meg. Ezután átadja a MethodCallExpression paraméter tulajdonsága által Provider képviselt metódusnak IQueryProvidersource.Execute<TResult>(Expression)

A meghívást ElementAt<TSource>(IQueryable<TSource>, Int32) jelképező kifejezésfa végrehajtása során előforduló lekérdezési viselkedés a paraméter típusának source implementálásától függ. A várt viselkedés az, hogy az elemet a következő pozícióban indexsourceadja vissza: .

A következőre érvényes: