ObjectQuery<T> 建構函式

定義

初始化 ObjectQuery<T> 類別的新執行個體。

多載

名稱 Description
ObjectQuery<T>(String, ObjectContext)

使用指定的 Entity SQL 指令作為初始查詢,建立一個新 ObjectQuery<T> 實例。

ObjectQuery<T>(String, ObjectContext, MergeOption)

使用指定的 Entity SQL 指令作為初始查詢,並以指定的合併選項建立一個新 ObjectQuery<T> 實例。

備註

An ObjectQuery<T> 可以初始化為代表單一純量結果,而非純量結果的集合。 部分擴展方法要求以收集結果作為輸入。 此時,當呼叫這些方法之一時,會拋出 a ArgumentException 。 如需詳細資訊,請參閱物件查詢

當你的應用程式在執行時產生實體 SQL 查詢時,你應該注意資料來源的指令長度限制。 實體 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

實體 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);
}

備註

當你的應用程式在執行時產生實體 SQL 查詢時,你應該注意資料來源的指令長度限制。 實體 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

實體 SQL 查詢。

context
ObjectContext

ObjectContext 執行查詢的基礎。

mergeOption
MergeOption

規定透過此查詢檢索的實體應如何與先前查詢 ObjectContext回傳的實體合併。

範例

在此範例中,初始ObjectQuery<T>化為指定的查詢、 ObjectContextMergeOption

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"]);
    }
}

備註

當你的應用程式在執行時產生實體 SQL 查詢時,你應該注意資料來源的指令長度限制。 實體 SQL 不強制執行查詢中指令文字長度的限制。

適用於