This documentation is archived and is not being maintained.

CompiledQuery.Compile Method

Creates a new delegate that represents the compiled query.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

The generic arguments to this method are (in order):

  • TDataContext. Constrained to be a DataContext. It represents the DataContext that must be passed in when executing the delegate returned by the Compile method.

  • TArg0, …, TArg2. Represents the types of the parameters that have to be passed in when executing the delegate returned by the Compile method.

  • TResult. Represents the type of T in the IEnumerable<T> returned when executing the delegate returned by the Compile method.

Passing a query expression that does not use the TDataContext parameter generates an exception when the returning delegate is executed for the first time.

The Return Value

Func<TDataContext, TArg0, TArg1, …, TArg2, TResult>, representing the delegate that can be cached. At the time when this delegate is executed, the query will be compiled. Observe the following notes:

  • Passing null to the first argument typed TDataContext generates a run-time exception.

  • It is an error to re-execute a compiled query using a DataContext that contains a DataLoadOptions different from the one originally used to compile the query (the first time it is executed). The reason for this behavior is that compiling the query requires information that is contained in the DataShape. When a query is executed again using a different DataShape, the cached query becomes invalid and an error is generated.

  • If a new query operator is applied to the result of the delegate execution, an exception is generated.

  • When you want to execute a query operator over the result of executing a compiled query, you must translate the result to a list before operating on it.

The following code example assumes a Queries class designed to store compiled queries, and assumes a Northwind class that represents a strongly typed DataContext.

public static Func<Northwnd, string, IQueryable<Customer>>
    CustomersByCity =
        CompiledQuery.Compile((Northwnd db, string city) =>
            from c in db.Customers where c.City == city select c);

public static Func<Northwnd, string, IQueryable<Customer>>
    CustomersById = CompiledQuery.Compile((Northwnd db,
    string id) => db.Customers.Where(c => c.CustomerID == id));
// The following example invokes such a compiled query in the main 
// program. 

public IEnumerable<Customer> GetCustomersByCity(string city)
    var myDb = GetNorthwind();
    return Queries.CustomersByCity(myDb, city);

You cannot currently store (in static variables) queries that return an anonymous type, because the type has no name to provide as a generic argument. The following example shows how you can work around the issue by creating a type that can represent the result, and then use it as a generic argument.

class SimpleCustomer
    public string ContactName { get; set; }

class Queries2
    public static Func<Northwnd, string, IEnumerable<SimpleCustomer>> CustomersByCity =
        CompiledQuery.Compile<Northwnd, string, IEnumerable<SimpleCustomer>>(
        (Northwnd db, string city) =>
        from c in db.Customers
        where c.City == city
        select new SimpleCustomer { ContactName = c.ContactName });