ObjectQuery(Of T) Class
Represents a typed query against an Entity Data Model (EDM) in a given object context.
Assembly: System.Data.Entity (in System.Data.Entity.dll)
The ObjectQuery(Of 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(Of 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(Of 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(Of 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(Of T) class.
Using advWorksContext As New AdventureWorksEntities Try ' Call the constructor that takes a command string and ObjectContext. Dim productQuery1 As New ObjectQuery(Of Product)("Product", advWorksContext) Dim result As Product For Each result In productQuery1 Console.WriteLine("Product Name: {0}", result.Name) Next Dim queryString As String = _ "SELECT VALUE Product FROM AdventureWorksEntities.Product AS Product" ' Call the constructor with the specified query and the ObjectContext. Dim productQuery2 As New ObjectQuery(Of Product)(queryString, advWorksContext) Dim result1 As Product For Each result1 In productQuery2 Console.WriteLine("Product Name: {0}", result1.Name) Next ' Call the constructor with the specified query, the ObjectContext, ' and the NoTracking merge option. Dim productQuery3 As New ObjectQuery(Of Product) _ (queryString, advWorksContext, MergeOption.NoTracking) Dim result2 As Product For Each result2 In productQuery3 Console.WriteLine("Product Name: {0}", result2.Name) Next Catch ex As EntitySqlException Console.WriteLine(ex.ToString) End Try End Using
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.