Queryable.ElementAt<TSource>(IQueryable<TSource>, Int32) Метод
Определение
Возвращает элемент по указанному индексу в последовательности.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
Параметры типа
- TSource
Тип элементов source
.The type of the elements of source
.
Параметры
- source
- IQueryable<TSource>
Объект IQueryable<T>, из которого требуется возвратить элемент.An IQueryable<T> to return an element from.
- index
- Int32
Отсчитываемый от нуля индекс извлекаемого элемента.The zero-based index of the element to retrieve.
Возвращаемое значение
- TSource
Элемент, находящийся в указанной позиции в последовательности source
.The element at the specified position in source
.
Исключения
source
имеет значение null
.source
is null
.
Значение параметра index
меньше нуля.index
is less than zero.
Примеры
В следующем примере кода показано, как использовать ElementAt<TSource>(IQueryable<TSource>, Int32) для возвращения элемента в определенной позиции последовательности.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'.
Комментарии
ElementAt<TSource>(IQueryable<TSource>, Int32)Метод создает объект MethodCallExpression , который представляет вызов ElementAt<TSource>(IQueryable<TSource>, Int32) самого себя как сконструированного универсального метода.The ElementAt<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling ElementAt<TSource>(IQueryable<TSource>, Int32) itself as a constructed generic method. Затем он передает MethodCallExpression Execute<TResult>(Expression) методу класса, IQueryProvider представленного Provider свойством source
параметра.It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
Поведение запроса, которое происходит в результате выполнения дерева выражения, представляющего вызов, ElementAt<TSource>(IQueryable<TSource>, Int32) зависит от реализации типа source
параметра.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. Ожидаемое поведение заключается в том, что он возвращает элемент в позиции index
в source
.The expected behavior is that it returns the item at position index
in source
.