Uredi

Deli z drugimi prek


Queryable.ElementAt Method

Definition

Overloads

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

Returns the element at a specified index in a sequence.

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

Returns the element at a specified index in a sequence.

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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, 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

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

An IQueryable<T> to return an element from.

index
Index

The index of the element to retrieve, which is either from the start or the end.

Returns

TSource

The element at the specified position in the source sequence.

Exceptions

source is null.

index is outside the bounds of the source sequence.

Applies to

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

An IQueryable<T> to return an element from.

index
Int32

The zero-based index of the element to retrieve.

Returns

TSource

The element at the specified position in source.

Exceptions

source is null.

index is less than zero.

Examples

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

Remarks

The ElementAt<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling ElementAt<TSource>(IQueryable<TSource>, Int32) itself as a constructed generic method. It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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. The expected behavior is that it returns the item at position index in source.

Applies to