开发者

Wcf Friendly PagedList

开发者 https://www.devze.com 2023-02-09 12:15 出处:网络
We have been working with various versions of the PagedList , however these pagedlists are not Wcf friendly. They do not serialize. Does anyone ha开发者_如何学Cve a good way to get paged data across W

We have been working with various versions of the PagedList , however these pagedlists are not Wcf friendly. They do not serialize. Does anyone ha开发者_如何学Cve a good way to get paged data across Wcf? (Including properties such as TotalRecords of the datasource.)


We worked out a solution to simplify the current PagedList to something that is more serializable and a little more geared to our solution. Instead of creating a custom List/Collection, we created a class that contains the list/collection.

Previously we were losing the custom properties of the pagedlist when it was deserialized on the client side. It still had all the items in the list, but it was missing the properties.

This is based on code by Troy Goode which I believe is based on some unreleased Microsoft MVC code. More info can be found here: http://www.squaredroot.com/2009/06/15/return-of-the-pagedlist/

 public class PagedList<T> : IPagedList<T>
    {
        public PagedList()
        {
        }

        public PagedList(IEnumerable<T> source, int pageIndex, int pageSize)
            : this(source == null ? new List<T>().AsQueryable() : source.AsQueryable(), pageIndex, pageSize)
        {
        }

        private PagedList(IQueryable<T> source, int pageIndex, int pageSize)            
        {
            TotalRecords = source.Count();

            // add items to internal list
            if (TotalRecords > 0)
                if (pageIndex == 0)
                    Data = source.Take(pageSize).ToList();
                else
                    Data = source.Skip((pageIndex) * pageSize).Take(pageSize).ToList();
        }

        public int TotalRecords { get; set; }

        public IEnumerable<T> Data { get; set; }
    }

and then the actual implementation through the following extension:

public static PagedList<T> ToPagedList<T>(this IEnumerable<T> source, int pageIndex, int pageSize)
{
    return new PagedList<T>(source, pageIndex, pageSize);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消