ObjectQuery<T> コンストラクター

定義

ObjectQuery<T> クラスの新しいインスタンスを初期化します。

オーバーロード

ObjectQuery<T>(String, ObjectContext)

指定した Entity SQL コマンドを最初のクエリとして使用して、新しい ObjectQuery<T> インスタンスを作成します。

ObjectQuery<T>(String, ObjectContext, MergeOption)

指定した Entity SQL コマンドを最初のクエリとして使用し、指定したマージ オプションを使用して、新しい ObjectQuery<T> インスタンスを作成します。

注釈

ObjectQuery<T> は、スカラー結果のコレクションではなく単一のスカラー結果を表すように初期化できます。 一部の拡張メソッドは、入力としてコレクション結果を必要とします。 その場合は、該当するメソッドのいずれかが呼び出されたときに、ArgumentException がスローされます。 詳しくは、「オブジェクト クエリ」をご覧ください。

アプリケーションが実行時に Entity SQL クエリを生成する場合は、データ ソースのコマンド長の制限に注意する必要があります。 Entity SQL では、クエリ内のコマンド テキストの長さに制限は適用されません。

ObjectQuery<T>(String, ObjectContext)

指定した Entity SQL コマンドを最初のクエリとして使用して、新しい ObjectQuery<T> インスタンスを作成します。

public:
 ObjectQuery(System::String ^ commandText, System::Data::Objects::ObjectContext ^ context);
public ObjectQuery (string commandText, System.Data.Objects.ObjectContext context);
new System.Data.Objects.ObjectQuery<'T> : string * System.Data.Objects.ObjectContext -> System.Data.Objects.ObjectQuery<'T>
Public Sub New (commandText As String, context As ObjectContext)

パラメーター

commandText
String

Entity SQL クエリ。

context
ObjectContext

クエリが実行される ObjectContext

この例では、 クラスのインスタンスを構築する方法を ObjectQuery<T> 示します。

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Call the constructor with a query for products and the ObjectContext.
    ObjectQuery<Product> productQuery1 =
        new ObjectQuery<Product>("Products", context);

    foreach (Product result in productQuery1)
        Console.WriteLine("Product Name: {0}", result.Name);

    string queryString =
        @"SELECT VALUE product FROM AdventureWorksEntities.Products AS product";

    // Call the constructor with the specified query and the ObjectContext.
    ObjectQuery<Product> productQuery2 =
        new ObjectQuery<Product>(queryString, context);

    foreach (Product result in productQuery2)
        Console.WriteLine("Product Name: {0}", result.Name);

    // Call the constructor with the specified query, the ObjectContext,
    // and the NoTracking merge option.
    ObjectQuery<Product> productQuery3 =
        new ObjectQuery<Product>(queryString,
            context, MergeOption.NoTracking);

    foreach (Product result in productQuery3)
        Console.WriteLine("Product Name: {0}", result.Name);
}

注釈

アプリケーションが実行時に Entity SQL クエリを生成する場合は、データ ソースのコマンド長の制限に注意する必要があります。 Entity SQL では、クエリ内のコマンド テキストの長さに制限は適用されません。

こちらもご覧ください

適用対象

ObjectQuery<T>(String, ObjectContext, MergeOption)

指定した Entity SQL コマンドを最初のクエリとして使用し、指定したマージ オプションを使用して、新しい ObjectQuery<T> インスタンスを作成します。

public:
 ObjectQuery(System::String ^ commandText, System::Data::Objects::ObjectContext ^ context, System::Data::Objects::MergeOption mergeOption);
public ObjectQuery (string commandText, System.Data.Objects.ObjectContext context, System.Data.Objects.MergeOption mergeOption);
new System.Data.Objects.ObjectQuery<'T> : string * System.Data.Objects.ObjectContext * System.Data.Objects.MergeOption -> System.Data.Objects.ObjectQuery<'T>
Public Sub New (commandText As String, context As ObjectContext, mergeOption As MergeOption)

パラメーター

commandText
String

Entity SQL クエリ。

context
ObjectContext

クエリが実行される ObjectContext

mergeOption
MergeOption

このクエリによって取得されるエンティティを、同じ ObjectContext に対する以前のクエリから返されたエンティティとどのようにマージするかを指定します。

この例では、 ObjectQuery<T> は、指定したクエリ、、 ObjectContextおよび を使用して MergeOption初期化されます。

int productID = 900;
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string queryString = @"SELECT VALUE product FROM
        AdventureWorksEntities.Products AS product
        WHERE product.ProductID > @productID";

    ObjectQuery<Product> productQuery1 =
        new ObjectQuery<Product>(queryString,
            context, MergeOption.NoTracking);

    productQuery1.Parameters.Add(new ObjectParameter("productID", productID));

    ObjectQuery<DbDataRecord> productQuery2 =
        productQuery1.Select("it.ProductID");

    foreach (DbDataRecord result in productQuery2)
    {
        Console.WriteLine("{0}", result["ProductID"]);
    }
}

注釈

アプリケーションが実行時に Entity SQL クエリを生成する場合は、データ ソースのコマンド長の制限に注意する必要があります。 Entity SQL では、クエリ内のコマンド テキストの長さに制限は適用されません。

適用対象