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);

Answers


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.

http://www.davidhayden.me/blog/asp.net-mvc-4-web-api-routes-and-apicontroller


[OperationContract]
IList<Customer> GetCustomers(int from, int numResults);

What is more inefficient? Changing your code in a few places, or sending your entire dataset across the 'net to get 10 rows?

Your best bet is usually to apply paging on the service side, where LINQ can perform an optimal query against the database. Even if the paging on the service sid isn't all that optimized, you will still gain by passing a smaller amount of data back to the consumer.

As for sorting, I see no reason you couldn't do sorting on the client side. In fact, I think you should, so that consumers can sort however they see fit. You won't gain or lose much from sorting - the same number of records are passed, sorted or not.


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?


Need Your Help

Windows Phone camera feed over UDP is "horribly" slow

c# udp windows-phone-7.1 asyncsocket

I have been working on a private project where i wanted to learn how to program on a windows phone, and at a point i started to fiddle with sockets and the camera, and a great idea came to mind video

GLSL variable turns object black

opengl glsl lwjgl

I currently have this vertex shader: