Silverlight Feeds - All your Silverlight feeds in one place.

Sponsors

Monday, January 31, 2011

WCF RIA Services and TotalEntitiesCount=-1

by Corrado Cavalli via Corrado's BLogs on 1/31/2011 6:47:51 AM

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.
 

email it!bookmark it!digg it!

Original Post: WCF RIA Services and TotalEntitiesCount=-1

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. Silverlight Feeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on Silverlight Feeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us