Paging with WCF data service

2019-02-26 21:13发布

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!

3条回答
祖国的老花朵
2楼-- · 2019-02-26 21:50

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.

查看更多
男人必须洒脱
3楼-- · 2019-02-26 21:51

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

查看更多
我只想做你的唯一
4楼-- · 2019-02-26 21:53

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.

查看更多
登录 后发表回答