Queryable.ElementAt 方法

定义

重载

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

返回序列中指定索引处的元素。

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

返回序列中指定索引处的元素。

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

返回序列中指定索引处的元素。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要从中返回元素的 IQueryable<T>

index
Index

要检索的元素的索引,从开头或结尾开始。

返回

TSource

序列中指定位置处的 source 元素。

例外

source 上声明的默认值为 null

index 位于序列边界 source 之外。

适用于

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

返回序列中指定索引处的元素。

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 的元素类型。

参数

source
IQueryable<TSource>

要从中返回元素的 IQueryable<T>

index
Int32

要检索的从零开始的元素索引。

返回

TSource

source 中指定位置处的元素。

例外

sourcenull

index 小于零。

示例

下面的代码示例演示如何用于 ElementAt<TSource>(IQueryable<TSource>, Int32) 返回序列中特定位置的元素。

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)生成一个表示调用ElementAt<TSource>(IQueryable<TSource>, Int32)本身为构造泛型方法的一个MethodCallExpression方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 ElementAt<TSource>(IQueryable<TSource>, Int32) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是它返回位于source位置index的项。

适用于