Editare

Partajați prin


ObjectQuery<T> Constructors

Definition

Initializes a new instance of the ObjectQuery<T> class.

Overloads

ObjectQuery<T>(String, ObjectContext)

Creates a new ObjectQuery<T> instance using the specified Entity SQL command as the initial query.

ObjectQuery<T>(String, ObjectContext, MergeOption)

Creates a new ObjectQuery<T> instance using the specified Entity SQL command as the initial query and the specified merge option.

Remarks

An ObjectQuery<T> can be initialized in such a way that it represents a single scalar result and not a collection of scalar results. Some extension methods require collection results as input. In this case, an ArgumentException is thrown when one of these methods is called. For more information, see Object Queries.

When your application generates Entity SQL queries at runtime, you should be aware of any command length limitations of the data source. Entity SQL does not enforce limitations on the length of the command text in queries.

ObjectQuery<T>(String, ObjectContext)

Creates a new ObjectQuery<T> instance using the specified Entity SQL command as the initial query.

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)

Parameters

commandText
String

The Entity SQL query.

context
ObjectContext

The ObjectContext on which to execute the query.

Examples

This example shows how to construct an instance of the ObjectQuery<T> class.

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

Remarks

When your application generates Entity SQL queries at runtime, you should be aware of any command length limitations of the data source. Entity SQL does not enforce limitations on the length of the command text in queries.

See also

Applies to

ObjectQuery<T>(String, ObjectContext, MergeOption)

Creates a new ObjectQuery<T> instance using the specified Entity SQL command as the initial query and the specified merge option.

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)

Parameters

commandText
String

The Entity SQL query.

context
ObjectContext

The ObjectContext on which to execute the query.

mergeOption
MergeOption

Specifies how the entities that are retrieved through this query should be merged with the entities that have been returned from previous queries against the same ObjectContext.

Examples

In this example, the ObjectQuery<T> is initialized with the specified query, ObjectContext, and 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"]);
    }
}

Remarks

When your application generates Entity SQL queries at runtime, you should be aware of any command length limitations of the data source. Entity SQL does not enforce limitations on the length of the command text in queries.

Applies to