Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

CompiledQuery.Compile<TArg0, TResult> Method (Expression<Func<TArg0, TResult>>)

Creates a new delegate that represents the compiled LINQ to Entities query.

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

public static Func<TArg0, TResult> Compile<TArg0, TResult>(
	Expression<Func<TArg0, TResult>> query
where TArg0 : ObjectContext

Type Parameters


A type derived from ObjectContext.


The type T of the query results returned by executing the delegate returned by the Compile method.


Type: System.Linq.Expressions.Expression<Func<TArg0, TResult>>
The lambda expression to compile.

Return Value

Type: System.Func<TArg0, TResult>
Func<T, TResult>, a generic delegate that represents the compiled LINQ to Entities query.

For more information and examples, see Compiled Queries.

The following example compiles and then invokes a query that returns an ObjectQuery<T> instance:

static readonly Func<AdventureWorksEntities, ObjectQuery<SalesOrderHeader>> s_compiledQuery1 = 
    CompiledQuery.Compile<AdventureWorksEntities, ObjectQuery<SalesOrderHeader>>(
            ctx => ctx.SalesOrderHeaders);

static void CompiledQuery1_MQ()

    using (AdventureWorksEntities context = new AdventureWorksEntities())
        IQueryable<SalesOrderHeader> orders = s_compiledQuery1.Invoke(context);

        foreach (SalesOrderHeader order in orders)

.NET Framework

Supported in: 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

© 2015 Microsoft