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 では、クエリ内のコマンド テキストの長さに制限は適用されません。

適用対象