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

取得する要素の、0 から始まるインデックス。The zero-based index of the element to retrieve.

戻り値

TSource TSource TSource TSource

indexsource の範囲外の場合は 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. これは、後、渡します、MethodCallExpressionExecute<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. 想定される動作は、位置にある項目が返されるindexsource、または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.

適用対象