views:

41

answers:

2

Here's the issue:

The database is highly normalized, and one particular query relies on the multiple relationships in the database. The query is designed to join all the tables, construct the entire object, and then return a list of those objects.

In other words this particular query does a lot of work.

Now, the query does only return X number of items as it supports pagination, but we also need to know the total count of items that are there.

Currently these two tasks are independent, but highly similar queries in our Domain Service. Ideally what I'd like to do is combine these two queries so that the call to the server happens once, rather than twice, and that the joins happen only once.

Output/Reference parameters don't work, and since the function is designed to return an IQueryable of items, I'm stuck on how to return this list of items as well as the total count.

I'm sure someone's come across this before - any thoughts?

A: 

If the problem is with the client server communication, you can put the count result on the header of the result response.

Chen Kinnrot
I'm not sure I can modify the response's header - keep in mind this is using RIA services, which auto-generates the web service as well as the objects on the client side, so that I don't have much ability to change the generated service. I know that with WCF services, output params are supported, as I've been able to do this before.
AlishahNovin
+1  A: 

A count of item joined tables is not the same thing as returning a subset of those records. They just happen to share a certain amount of SQL code (specifically to join the tables). RIA does the actual paging server-side so you are actually getting a slightly different query for every paging call.

A count operation would also operate much faster than the record query as SQL counts can often be performed using database indexes only (although Linq may well optimise this for you to the same end result... Clever Linq coders!).

As you would only be requesting the total count once (on page load I assume), then you begin paging through multiple queries on different portions of the data, you are hitting different parts of the database with every call.

You are better off treating them as two distinct functions (as you were) and wear the slight overhead of an additional server call. There is always somewhere else you could make bigger gains (caching etc).

When in doubt: Do not overcomplicate any process for the sake of only a very small gain.

Enough already
AlishahNovin
@AlishahNovin: Yes, I was assuming you were taking advantage of the server-side Linq-evaluation feature of RIA. Without seeing your code (and in light of your not making standard use of RIA), I can't really help any further. Good luck with your project anyway.
Enough already