Queryable.ElementAtOrDefault<TSource> メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
シーケンス内の指定したインデックス位置にある要素を返します。インデックスが範囲外の場合は既定値を返します。
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の要素の型。
パラメーター
- source
- IQueryable<TSource>
要素を返す IQueryable<T> 。
- index
- Int32
取得する要素の 0 から始まるインデックス。
返品
default(TSource) index が sourceの境界外にある場合は 。それ以外の場合は、 source内の指定した位置にある要素。
例外
source は nullです。
例
次のコード例は、 ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32)の使用方法を示しています。 この例では、ソース シーケンスの境界外にある index に値を使用します。
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)を生成します。 次に、MethodCallExpression パラメーターのExecute<TResult>(Expression) プロパティで表されるIQueryProviderのProvider メソッドにsourceを渡します。
呼び出し ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32) を表す式ツリーを実行した結果として発生するクエリ動作は、 source パラメーターの型の実装によって異なります。 予想される動作は、index内のsource位置にある項目を返すか、default(TSource)がindexの範囲外にある場合にsourceすることです。