views:

183

answers:

1

Hi guys

I have started using linq to sql and the entity framework and have found the development experience to be fairly good. Like the way you can break a query apart and combine different queries is quite nice - see pipes and filters.

But the problem that I have found is that performance can be greatly increased (in my case by a factor of about 4 which I can't ignore) if I use complied queries. But the problem I have found with compiled quires is that they don't like working with IQueryable...

Does anyone have any ideas on how I can get round this shortcoming??? The way I was working is that the Compiled Query just referenced other queries that used IQueryable, that way I could switch in and out of using compiled quires or not. But as I have found out this doesn't work so well.

Any ideas?

Cheers Anthony

A: 

No, you cannot compose the results of CompiledQuery with another IQueryable. Remember, the sole purpose of CompiledQuery is to cache the results of transforming an IQueryable into an Entity Framework canonical command tree. If you could then compose this with another IQueryable, then it would need to be recompiled for execution, which completely defeats the purpose of CompiledQuery.

Craig Stuntz
So does that means that if you want to use compiled queries there is no way reusing logic in the say way you would if you were not using compiled queries and using pipes and filters?
vdh_ant
It really means that CompiledQuery is very much like a prepared SQL statement. In other words, you can change argument (parameter) values, but you cannot compose this statement with other SQL operators. This makes sense, since the purpose of SQL statement preparation is to ensure that you only incur the overhead of query parsing and server resource allocation once for a statement which you will execute multiple times (potentially, with different parameter values). CompiledQuery is the same thing, only for Expressions.
Craig Stuntz
I know what your saying but wouldn't the more logical thing to do would be to evaluate the expression the first time, allowing for the pipes and filters and then cache the results...
vdh_ant
The EF already does that for non-compiled queries over the lifespan of a single ObjectContext. You don't need CompiledQuery for that feature.
Craig Stuntz