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);
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.
例外
source 为 null。source 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 给 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.
由于执行表示调用的表达式树而发生的查询行为 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. 预期的行为是在中的位置返回项 index source ,或者, default(TSource) 如果超出 index 的界限, source 则返回。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.