Export (0) Print
Expand All

ObjectQuery<T> Class

Represents a typed query against an Entity Data Model (EDM) in a given object context.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public class ObjectQuery<T> : ObjectQuery, 
	IOrderedQueryable<T>, IQueryable<T>, IEnumerable<T>, IOrderedQueryable, IQueryable, 
	IEnumerable, IListSource

Type Parameters

T

The entity type of the query.

The ObjectQuery<T> generic class represents a query that returns a collection of zero or more objects of a specific type. The ObjectContext must be defined before an object query can be executed. This context provides the connection and metadata information that is required to compose and execute the query. An object query is executed in the following scenarios:

  • When it is acted upon, such as during a foreach (C#) or For Each (Visual Basic) enumeration.

  • When it is assigned to fill a List<T> collection.

  • When the Execute method is explicitly called.

The query itself can be created by using an Entity SQL statement or a LINQ query, or it can be incrementally built by using the query builder methods included in the class. In every case, the result is a new ObjectQuery<T> instance that, when explicitly executed or enumerated over, will send the query to the data source for execution and return the results.

An ObjectQuery<T> is frequently of an entity type, but it can also be of DbDataRecord type, for projections to an anonymous type, or of a primitive type, such as an integer or string. For more information, see Object Queries (Entity Framework).

The example in this topic is based on the Adventure Works Sales Model.

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

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

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

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

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

        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, 
                advWorksContext, MergeOption.NoTracking);

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

System.Object
  System.Data.Objects.ObjectQuery
    System.Data.Objects.ObjectQuery<T>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Community Additions

ADD
Show:
© 2014 Microsoft