Queryable.ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) 方法

定义

返回序列中指定索引处的元素;如果索引超出范围,则返回默认值。Returns the element at a specified index in a sequence or a default value if the index is out of range.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource ElementAtOrDefault(System::Linq::IQueryable<TSource> ^ source, int index);
public static TSource ElementAtOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, int index);
static member ElementAtOrDefault : System.Linq.IQueryable<'Source> * int -> 'Source
<Extension()>
Public Function ElementAtOrDefault(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

如果 index 超出 source 的界限,则返回 default(TSource);否则返回 source 中指定位置处的元素。default(TSource) if index is outside the bounds of source; otherwise, the element at the specified position in source.

异常

sourcenullsource is null.

示例

下面的代码示例演示如何使用 ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32)The following code example demonstrates how to use ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32). 此示例使用超出源序列界限的 index 的值。This example uses a value for index that is outside the bounds of the source sequence.

string[] names = { "Hartono, Tommy", "Adams, Terry",
                   "Andersen, Henriette Thaulow",
                   "Hedlund, Magnus", "Ito, Shu" };

int index = 20;

string name = names.AsQueryable().ElementAtOrDefault(index);

Console.WriteLine(
    "The name chosen at index {0} is '{1}'.",
    index,
    String.IsNullOrEmpty(name) ? "[NONE AT THIS INDEX]" : name);

/*
    This code produces the following output:

    The name chosen at index 20 is '[NONE AT THIS INDEX]'.
*/

Dim names() As String = {"Hartono, Tommy", "Adams, Terry", _
                   "Andersen, Henriette Thaulow", _
                   "Hedlund, Magnus", "Ito, Shu"}

Dim index As Integer = 20

Dim name As String = names.AsQueryable().ElementAtOrDefault(index)

MsgBox(String.Format("The name at index {0} is '{1}'.", _
    index, IIf(String.IsNullOrEmpty(name), "[NONE AT THIS INDEX]", name)))

' This code produces the following output:
'
' The name at index 20 is '[NONE AT THIS INDEX]'.

注解

ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) 方法生成一个 MethodCallExpression,它表示作为构造的泛型方法调用 ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) 本身。The ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) itself as a constructed generic method. 然后,它将 MethodCallExpression 传递到 IQueryProviderExecute<TResult>(Expression) 方法(由 source 参数的 Provider 属性表示)。It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

因执行表示调用 ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) 的表达式树而发生的查询行为取决于 source 参数类型的实现。The query behavior that occurs as a result of executing an expression tree that represents calling ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) depends on the implementation of the type of the source parameter. 预期的行为是,它在 sourceindex 位置返回项; 如果 index 超出 source的界限,则返回 default(TSource)The expected behavior is that it returns the item at position index in source, or default(TSource) if index is outside the bounds of source.

适用于