CompiledQuery.Compile<TArg0, TArg1, TResult> Method (Expression<Func<TArg0, TArg1, 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, TArg1, TResult> Compile<TArg0, TArg1, TResult>(
	Expression<Func<TArg0, TArg1, TResult>> query
where TArg0 : ObjectContext


Type: System.Linq.Expressions.Expression<Func<TArg0, TArg1, TResult>>

The lambda expression to compile.

Return Value

Type: System.Func<TArg0, TArg1, TResult>

Func<T1, T2, TResult>, a generic delegate that represents the compiled LINQ to Entities query.

Type Parameters


A type derived from ObjectContext.


Represents the type of the parameter that has to be passed in when executing the delegate returned by this method. TArg1must be a primitive type.


The type T of the query results returned by executing the delegate returned by the Compile<TArg0, TArg1, TResult> method.

For more information and examples, see Compiled Queries (LINQ to Entities).

The following example compiles and then invokes a query that accepts a Decimal input parameter and returns a sequence of orders where the total due is greater than or equal to $200.00:

static readonly Func<AdventureWorksEntities, Decimal, IQueryable<SalesOrderHeader>> s_compiledQuery2 = 
    CompiledQuery.Compile<AdventureWorksEntities, Decimal, IQueryable<SalesOrderHeader>>(
            (ctx, total) => from order in ctx.SalesOrderHeaders
                            where order.TotalDue >= total
                            select order);

static void CompiledQuery2()
    using (AdventureWorksEntities context = new AdventureWorksEntities())
        Decimal totalDue = 200.00M;

        IQueryable<SalesOrderHeader> orders = s_compiledQuery2.Invoke(context, totalDue);

        foreach (SalesOrderHeader order in orders)
            Console.WriteLine("ID: {0}  Order date: {1} Total due: {2}",

.NET Framework
Available since 3.5
Return to top