ASP.NET with WCF Services - how to page data
I have a WCF service with functions that return lists, such as one to return a list of customers
[OperationContract] IList<Customer> GetCustomers();
My ASP.NET page may do:
var top10Customers = Model.GetCustomers().Take(10); var first10Customers = Model.OrderBy(c => c.ID).Take(10);
It is my understanding that the following code woulld return all Customers from the WCF service and THEN apply OrderBy and Take(10)
It seems inefficient to return the entire data set in one go, especially as the WCF service may reside on a different machine to the ASP.NET website. In which case how best can I page the results set or allow LINQ operations on it?
The only option I can see at the minute is to add paging options to the function, but this seems inefficient to re-write this code all the time:
[OperationContract] IList<Customer> GetCustomers(int from, int numResults);
I'd suggest adding the parameters to the GetCustomers method and then execute the LINQ on that side in order to generate the most efficient SQL?
[OperationContract] IList<Customer> GetCustomers(int fromRecoard, string orderBy);
The order by will be a little trick, since you'll need to convert from a sting into a lamda expression and use the property of Customer, but there are plenty of samples on the web.
For more info on IQueryable in .Net 4.5 have a look at this blog post under the OData section.