Imagine the following case where a system manages all calls into a company.

When any call comes in, a Call entity is created. If the call is a "valid call" (the definition of "valid" is irrelevant), Call.Valid is set to true. If the call is a "valid call" AND such a call requires notes to be logged, then Call.Notes is set to the notes required for the call.

I would like to execute a batch query that returns the # of calls, the # of those calls that are "valid calls", and the # of those "valid calls" that have notes. It is important to note that each query is a subset of its previous query (i.e. a call can have notes if and only if it is a valid call), and also that each query is a scalar query (count).

I would also like to avoid repeating parts of LINQ statements that are the same for each of the three queries.

// Create queries
var callsQuery = Query.All<Call>();
var validCallsQuery = callsQuery.Where(call => call.Valid);
var callsWithNotes = validCallsQuery.Where(call => call.Notes != null);

// Batch queries
var callsCount = Query.ExecuteFutureScalar(() => callsQuery);
var validCallsCount = Query.ExecuteFutureScalar(() => validCallsQuery);
var callsWithNotesCount = Query.ExecuteFutureScalar(() => callsWithNotesQuery);

// Do something with the count values => this should generate one batched query

My questions are: 1. Would the overload for ExecuteFutureScalar that accepts a "key" parameter help me here? What affect does the "query caching" implemented by this overload have? 2. Is there a way to avoid "Create queries" portion (assigning queries to variables)? I ask because for my project there are actually more than 3 subsets of data (there are 12), and if possible, I'd like to avoid creating 12 query variables, then creating 12 "result" variables.


Updated at 12.07.2010 7:44:44

Btw, another question:

The approach outlined above will issue one query with three select statements (one for each Count result required). Since each count is a subset of the previous count, is there any reasonable way to have DO generate one statement that can return all three counts?

This thread was imported from our support forum. The original discussion may contain more detailed answer.

asked Jul 12 '10 at 07:16

ara's gravatar image


One Answer:

1. No, it won't.

Compiled queries work as follows:

  • If compiled query is already cached for the specified key, we run it. If key is not provided, we use MethodInfo of anonymous delegate you pass there as such key.

  • If compiled query isn't yet cached, we invoke the delegate you provide, it returns IQueryable (or invokes our query execution infrastructure via Queryable.Count-like method, and we extract the query it runs indirectly), we compile it and cache the result.

  • Finally, we run cached compilation result (compiled query).

So key won't help here, but this info might help you to make such decisions further.

2. I fear there is no such way. Probably, Expression.Invoke could help here, but currently we don't support it.

3. See viewtopic.php?f=29&t=6013 - this might help.

answered Jul 12 '10 at 19:19

Alex%20Yakunin's gravatar image

Alex Yakunin

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview


Once you sign in you will be able to subscribe for any updates here



Asked: Jul 12 '10 at 07:16

Seen: 3,277 times

Last updated: Jul 12 '10 at 07:16

powered by OSQA