Queryable.ElementAt(IQueryable<TSource>, Int32) Queryable.ElementAt(IQueryable<TSource>, Int32) Queryable.ElementAt(IQueryable<TSource>, Int32) Queryable.ElementAt(IQueryable<TSource>, Int32) Method

Definizione

Restituisce l'elemento in corrispondenza dell'indice specificato in una sequenza.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

Parametri di tipo

TSource

Tipo degli elementi di source.The type of the elements of source.

Parametri

source
IQueryable<TSource>

Un IQueryable<T> dal quale restituire un elemento.An IQueryable<T> to return an element from.

index
Int32 Int32 Int32 Int32

Indice in base zero dell'elemento da recuperare.The zero-based index of the element to retrieve.

Restituisce

TSource TSource TSource TSource

L'elemento in corrispondenza della posizione specificata in source.The element at the specified position in source.

Eccezioni

Esempi

Esempio di codice seguente viene illustrato come utilizzare ElementAt<TSource>(IQueryable<TSource>, Int32) per restituire un elemento in una posizione specifica in una sequenza.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'.

Commenti

Il ElementAt<TSource>(IQueryable<TSource>, Int32) metodo genera un MethodCallExpression che rappresenta una chiamata ElementAt<TSource>(IQueryable<TSource>, Int32) stesso come un metodo generico costruito.The ElementAt<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling ElementAt<TSource>(IQueryable<TSource>, Int32) itself as a constructed generic method. Passa quindi il MethodCallExpression per il Execute<TResult>(Expression) metodo del IQueryProvider rappresentato dal Provider proprietà del source parametro.It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

Il comportamento delle query che si verifica in seguito all'esecuzione di un albero delle espressioni che rappresenta una chiamata ElementAt<TSource>(IQueryable<TSource>, Int32) dipende dall'implementazione del tipo del source parametro.The 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. Il comportamento previsto è che restituisce l'elemento nella posizione index in source.The expected behavior is that it returns the item at position index in source.

Si applica a