views:

30

answers:

1

I have a fairly complex LINQ to Entities query I'd like to try compiling because it's a bit slower than I'd like.

I build it in a series of steps though. Here's a simple example:

public static List<Employee> GetEmployees(EntityContext ctx, bool showTerminated)
{
    var q = ctx.Employees;

    if(showTerminated==false)
    {
        q = q.Where(e => e.TerminationDt == null);
    }

    //...more conditional filters / Group By / Select applied...

    return q.ToList();
}

Is there any way to take advantage of compiled queries when you have this type of conditional query composition?

A: 

No. However, you could rewrite the query into a compilable version:

var q = ctx.Employees.Where(e => showTerminated || !e.TerminationDt.HasValue);
// rest of method
Craig Stuntz
var q = ctx.Employees.Where(e => !showTerminated || !e.TerminationDt.HasValue);
Gnostus
@Gnostus, that's not right. Read it again.
Craig Stuntz