Paging with WCF data service

2019-02-26 21:43发布

问题:

My question is about how you handle paging with a WCF data service. The way I want to use it, is execute a query (passing page size and current page), and get back the results of that query and also the paging information like total number of pages, current page number and page size. This paging information is used by the client (which is another service that transforms the result to JSON for a mobile application that consumes it) to handle next/previous buttons.

However, using LINQ on a WCF data service is too limited, it doesn't support the LINQ expression I need.

I tried creating a service operation in the WCF data service, but I can only return IQueryable collections of data entities, so I cannot return a custom entity that also contains paging information.

Is there a way to do implement paging for a WCF data service so that I next to the result I also get back paging information?

EDIT: because of the limitations of WCF data services, I switched to a normal WCF service. To be honest, I don't see why anyone would ever want to use a data service with these severe limitations!

回答1:

Unfortunately it seems that WCF data services is way too limited, and the solution for me was to switch to a regular WCF service so that I could use full LINQ and define data contracts myself.



回答2:

Have a look at the Paging Provider for WCF Data Services here and here



回答3:

Use Skip and Take to perform client side paging of data from the WCF data service, such as:

var items = (from i in ctx.MyEntities
             select i).Skip(StartIndex).Take(PageSize)

Where StartIndex is the beginning position of the data you want returned and PageSize is the number of elements max to return.