Queryable.ElementAtOrDefault(IQueryable<TSource>, Int32) Queryable.ElementAtOrDefault(IQueryable<TSource>, Int32) Queryable.ElementAtOrDefault(IQueryable<TSource>, Int32) Queryable.ElementAtOrDefault(IQueryable<TSource>, Int32) Method

定義

傳回位於序列中指定索引處的元素;如果索引超出範圍,則傳回預設值。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 Int32 Int32 Int32

要擷取的項目之以零為起始索引。The zero-based index of the element to retrieve.

傳回

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

例外狀況

範例

下列程式碼範例將示範如何使用 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)至由Provider source參數IQueryProvider的屬性所表示之的方法。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.

適用於