I have a query that I’d like to pre-compile, however the query is being performed on an already existing ObjectQuery as follows:-
ObjectQuery<Books> books = _ctx.Books.Include("Authors");
books = books.Where(book=>book.Vendor.Listing.Select(vend=>vend.Price > 200));
I would like to use a compiled query for the second query, however that would entail passing the “books” object in to the Compiled Query’s “Invoke” method which when I attempt to do throws an error saying that “Only scalar types are supported” since “books” is of type ObjectQuery.
So I can’t really do something like this:-
var results = QueryCache.GetVendorFilter.Invoke(context,books);
where, GetVendorFilter is a compiled query.
Is there any other way to have this query be compiled ? Does anyone know if EF4 has this limitation ?
My understanding is that a compiled query basically pre-builds the SQL statement for your query expression, but leaves the parameter values undefined. Since SQL doesn’t support using another query as a parameter, it makes sense to me that you can’t do this.
One thing to be aware of with precompiled queries is that the moment you do anything to them, they become “just another query,” which has to be recompiled when you evaluate it. So even if you just put a call to
.Take(10)at the end, you’ve lost the value of the precompiled query.Since your
booksquery is pretty much fixed, it should be easy to create a separate precompiled query for this particular scenario, though.