次の方法で共有


Queryable.First メソッド

定義

シーケンスの最初の要素を返します。

オーバーロード

名前 説明
First<TSource>(IQueryable<TSource>)

シーケンスの最初の要素を返します。

First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

指定した条件を満たすシーケンスの最初の要素を返します。

First<TSource>(IQueryable<TSource>)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

シーケンスの最初の要素を返します。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource First(System::Linq::IQueryable<TSource> ^ source);
public static TSource First<TSource>(this System.Linq.IQueryable<TSource> source);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource First<TSource>(this System.Linq.IQueryable<TSource> source);
static member First : System.Linq.IQueryable<'Source> -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member First : System.Linq.IQueryable<'Source> -> 'Source
<Extension()>
Public Function First(Of TSource) (source As IQueryable(Of TSource)) As TSource

型パラメーター

TSource

sourceの要素の型。

パラメーター

source
IQueryable<TSource>

最初の要素を返す IQueryable<T>

戻り値

TSource

sourceの最初の要素。

属性

例外

sourcenullです。

ソース シーケンスが空です。

次のコード例では、 First<TSource>(IQueryable<TSource>) を使用してシーケンス内の最初の要素を返す方法を示します。

int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
                    83, 23, 87, 435, 67, 12, 19 };

int first = numbers.AsQueryable().First();

Console.WriteLine(first);

/*
    This code produces the following output:

    9
*/
Dim numbers() As Integer = {9, 34, 65, 92, 87, 435, 3, 54, _
                    83, 23, 87, 435, 67, 12, 19}

Dim first As Integer = numbers.AsQueryable().First()

MsgBox(first)

' This code produces the following output:
'
' 9

注釈

First<TSource>(IQueryable<TSource>) メソッドは、構築されたジェネリック メソッドとしての呼び出しFirst<TSource>(IQueryable<TSource>)自体を表すMethodCallExpressionを生成します。 次に、source パラメーターのProvider プロパティで表されるIQueryProviderExecute<TResult>(Expression) メソッドにMethodCallExpressionを渡します。

呼び出し First<TSource>(IQueryable<TSource>) を表す式ツリーを実行した結果として発生するクエリ動作は、 source パラメーターの型の実装によって異なります。 予期される動作は、 sourceの最初の要素を返すということです。

適用対象

First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

指定した条件を満たすシーケンスの最初の要素を返します。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource First(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static TSource First<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource First<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member First : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member First : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
<Extension()>
Public Function First(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As TSource

型パラメーター

TSource

sourceの要素の型。

パラメーター

source
IQueryable<TSource>

要素を返す IQueryable<T>

predicate
Expression<Func<TSource,Boolean>>

条件の各要素をテストする関数。

戻り値

TSource

predicateでテストに合格するsourceの最初の要素。

属性

例外

source または predicatenull

predicateの条件を満たす要素はありません。

-又は-

ソース シーケンスが空です。

次のコード例では、 First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) を使用して、条件を満たすシーケンスの最初の要素を返す方法を示します。

int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
                  83, 23, 87, 435, 67, 12, 19 };

// Get the first number in the array that is greater than 80.
int first = numbers.AsQueryable().First(number => number > 80);

Console.WriteLine(first);

/*
    This code produces the following output:

    92
*/
Dim numbers() As Integer = {9, 34, 65, 92, 87, 435, 3, 54, _
                  83, 23, 87, 435, 67, 12, 19}

' Get the first number in the array that is greater than 80.
Dim first As Integer = numbers.AsQueryable().First(Function(number) number > 80)

MsgBox(first)

' This code produces the following output:
'
' 92

注釈

このメソッドには、型引数がFunc<T,TResult>型の 1 つである型Expression<TDelegate>のパラメーターが少なくとも 1 つ含まれています。 これらのパラメーターでは、ラムダ式を渡すことができます。ラムダ式は Expression<TDelegate>にコンパイルされます。

First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) メソッドは、構築されたジェネリック メソッドとしての呼び出しFirst<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)自体を表すMethodCallExpressionを生成します。 次に、source パラメーターのProvider プロパティで表されるIQueryProviderExecute<TResult>(Expression) メソッドにMethodCallExpressionを渡します。

呼び出し First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) を表す式ツリーを実行した結果として発生するクエリ動作は、 source パラメーターの型の実装によって異なります。 想定される動作は、predicateで指定された条件を満たすsourceの最初の要素を返します。

適用対象