Hi There,
I am currently building a simple ASP.NET MVC site using Linq to Entities. My first foray into this world was nerd dinner, which is where I found the paginated list code that I am attempting to use. The code looks as follows:
public class PaginatedList<T> : List<T>
{
public int PageIndex { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public int TotalPages { get; private set; }
public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
PageSize = pageSize;
TotalCount = source.Count();
TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
}
public bool HasPreviousPage
{
get
{
return (PageIndex > 0);
}
}
public bool HasNextPage
{
get
{
return (PageIndex + 1 < TotalPages);
}
}
}
The problem is I am using the entity framework, and the above code throws the following error:
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.
Not knowing the ins and outs of Linq, I'm not sure how to add in an orderby clause when I don't know what the column names will be in the above method, what with it being generic.
Thanks!