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.

例外狀況

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 給以 Execute<TResult>(Expression) 參數的屬性所表示的方法 IQueryProvider Provider sourceIt 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) 取決於參數類型的實作為 sourceThe 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 專案 sourcedefault(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.

適用於