.NET Framework Class Library
ObjectQuery<(Of <(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)
Syntax

Visual Basic (Declaration)
Public Class ObjectQuery(Of T) _
    Inherits ObjectQuery _
    Implements IOrderedQueryable(Of T), IQueryable(Of T),  _
    IEnumerable(Of T), IOrderedQueryable, IQueryable, IEnumerable, IListSource
Visual Basic (Usage)
Dim instance As ObjectQuery(Of T)
C#
public class ObjectQuery<T> : ObjectQuery, 
    IOrderedQueryable<T>, IQueryable<T>, IEnumerable<T>, IOrderedQueryable, IQueryable, 
    IEnumerable, IListSource
Visual C++
generic<typename T>
public ref class ObjectQuery : public ObjectQuery, 
    IOrderedQueryable<T>, IQueryable<T>, IEnumerable<T>, IOrderedQueryable, IQueryable, 
    IEnumerable, IListSource
JScript
JScript does not support generic types or methods.

Type Parameters

T

The entity type of the query.

Remarks

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).

Examples

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.

Visual Basic
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
C#
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());
    }
}
Inheritance Hierarchy

System..::.Object
  System.Data.Objects..::.ObjectQuery
    System.Data.Objects..::.ObjectQuery<(Of <(T>)>)
Thread Safety

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

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.
Version Information

.NET Framework

Supported in: 3.5 SP1
See Also

Reference

Other Resources

Tags :


Page view tracker