We have been working with various versions of the PagedList , however these pagedlists are not Wcf friendly. They do not serialize. Does anyone have a good way to get paged data across Wcf? (Including properties such as TotalRecords of the datasource.)
1 Answer
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);
}
-
I've been trying to use the code in your solution to serialize the IPagedList results from my WCF service on the client but with no success, can you please help me out? I'm not sure if its ideal for me to create a new question to explain in problem in-depth. Feb 6, 2013 at 9:04