I'm running a number of CAML queries against a large list in SharePoint 2010, and displaying the results in a gridview to the end user. I wish to page through the results returned by the query to improve performance. However, I am required to display a total count of the items returned by the query on the paging control. My question is, how can I determine the total number of items that will be returned by each query without actually returning them all in a single SPListItemCollection? To be precise, I wish to page through the results 10 items at a time; how can I do this and still have a total count of all items returned by the query?
Update
So far, none of the answers given have addressed my question - as such, I'm offering a bounty. I need to be able to get a total count of the number of items that a CAML query will return without having to run the query and return all of the items. This will enable me to display this total count value to the end user (a set requirement), while paging through the items collection to display a specific page of results in a gridview. This would avoid a massive performance hit for large lists on the page containing the gridview first loading.
If no one offers a valid answer to the above, I will accept an answer that gives a link to an MSDN article that explicitly says that the above functionality cannot be implemented.
Thanks, MagicAndi.
Unfortunately, I don't think you're going to get a definitive answer from MSDN saying what isn't possible... only what is. Here is the entire CAML Query Schema page on msdn. There is no mention of any type of "count" function there.
As for solving your problem, first you need to choose the correct method for finding items. For example, here are a couple cases:
Second, you can implement some sort of caching in your web part to reduce the time it takes to sort/page/filter. I like using the System.Web.HttpRuntime.Cache
to store a DataTable
(makes it easy to change the method for retrieving items while your grid can keep using the datatable). You could also use Page.Session
instead of caching.