WCF DataServices and Total Number of Rows
Cristian Merighi ()

OData protocol and ASP.NET Ajax 4 tools: how to use them in order to retrieve, along with a subset of rows, the total row number of elements stored in database.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.
ASP.NET Ajax 4 just came out of the box in its final vest, and white papers don't offer - so far - omnicomprehensive support for all the available features.
That's why I decided to pin newly discovered nuggets, as soon as I get 'em, here on my online diary.
This article is about data-paging (generally speaking). In particular it's about retrieving the total number of rows (stored in database and given a particular filter clause)
via WCF Data Services using
OData protocol URI conventions.
We're going to use retrieve data from a client browser through javascript scripts.
Let's start by loading necessary scripts (à la ASP.Net 4, I assume that the following code would result clear, otherwise please refer to this documentation or similar).
<script type="text/javascript">
//<![CDATA[
// jQuery has been already (statically) loaded
Sys.require([Sys.components.openDataServiceProxy, Sys.components.dataView]);
//]]>
</script>
Now the call to a System.Data.Services.DataService that exposes data in a "RESTful" fashion.
<script type="text/javascript">
//<![CDATA[
var totalCount;
Sys.onReady(function () {
$().dataView.defaults = {autoFetch: true, itemTemplate: '#dataViewItemTemplate'};
var proxy = $.openDataServiceProxy('<%=ResolveClientUrl("~/services/modelservice.svc") %>');
var topRows = 3;
var qb = new Sys.Data.OpenDataQueryBuilder('BudgetSet');
qb.set_inlineCount('allpages');
qb.set_orderby('EndDate desc,StartDate');
qb.set_top(topRows);
qb.set_filter("EndDate le datetime'<%=DateTime.Today.ToString("yyyy-MM-dd") %>'");
proxy.query(qb.toString(),
function(results, ctx, uri, c){
totalCount = c.count;
$("#expiredBudgets").dataView({ data: results });
});
});
//]]>
</script>
The interface implementation is omitted, how you will exploit the total row count information is less important than how to get it!
Hope it helped.
Take care. Bye.