This documentation is archived and is not being maintained.

ObjectContext.CreateQuery<T> Method

Creates an ObjectQuery<T> in the current object context by using the specified query string.

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

public ObjectQuery<T> CreateQuery<T>(
	string queryString,
	params ObjectParameter[] parameters
)

Type Parameters

T

The entity type of the returned ObjectQuery<T>.

Parameters

queryString
Type: System.String

The query string to be executed.

parameters
Type: System.Data.Objects.ObjectParameter[]

Parameters to pass to the query.

Return Value

Type: System.Data.Objects.ObjectQuery<T>
An ObjectQuery<T> of the specified type.

ExceptionCondition
ArgumentNullException

The queryString or parameters parameter is null.

Use CreateQuery<T> to create an ObjectQuery<T> of the specified type that belongs to the current object context.

This example is based on the Adventure Works Sales Model.

The example creates a simple query and iterates through the collection of results.

using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    try
    {
        string queryString =
            @"SELECT VALUE Contact FROM AdventureWorksEntities.Contact 
                AS Contact WHERE Contact.FirstName = @fn";

        ObjectQuery<Contact> contactQuery =
            advWorksContext.CreateQuery<Contact>(queryString,
                new ObjectParameter("fn", "Frances"));

        // Iterate through the collection of Contact items. 
        foreach (Contact result in contactQuery)
            Console.WriteLine("First Name: {0}, Last Name: {1}",
            result.FirstName, result.LastName);
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

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
Show: