If you do serious WCF RIA Services development you’ll probably end up with something like this:

using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;

[EnableClientAccess()]
public class NorthwindService : DomainService
{
private readonly NorthwindEntities datacontext = new NorthwindEntities();

[Query]
public IQueryable<Customers> GetCustomers()
{
return this.datacontext.Customers;
}
}

where code that interacts with data store is separated in a separate NortwindEntities class (maybe just because we use NHybernate as favorite ORM…).
Everything works as expected except for a small detail: Let’s suppose to add some client side paging: (you can follow this solution if you’re using RIA Services SP1)
 
dataGrid1.ItemsSource = this.context.Customers;
EntityQuery<Customers> query = this.context.GetCustomersQuery();
query = query.Skip(page).Take(size);
this.context.Load(query, lop =>
{
int tot = lop.TotalEntityCount;
}, null);
 
you know that paging sometimes involves requesting the total count of the entities so that we can calculate how many pages to expect,  this requires to slightly modify the query adding IncludeTotalCount=true
 
EntityQuery<Customers> query = this.context.GetCustomersQuery();
query = query.Skip(2).Take(3);
query.IncludeTotalCount = true;
 
Unfortunately running the query we’ll discover that TotalEntitiesCount always returns –1.
 
image

To have it working we need to modify the service overriding Count method so that it will return the total number of entities returned by the query:
 
[EnableClientAccess()]
public class NorthwindService : DomainService
{
private readonly NorthwindEntities datacontext = new NorthwindEntities();

protected override int Count<T>(IQueryable<T> query)
{
return query.Count();
}

[Query]
public IQueryable<Customers> GetCustomers()
{
return this.datacontext.Customers;
}
}

Now everything will behave as expected.