I am currently creating a simple ASP.NET MVC site using Linq to Entities. My first foray into this world was a botanical dinner, where I found the broken list code that I am trying to use. The code is 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 that I am using an entity structure and the above code causes the following error:
The Skip method is only supported for sorted input in LINQ to Entities. The OrderBy method must be called before the Skip method.
Not knowing Linq's inputs and outputs, I'm not sure how to add an orderby clause when I don't know what column names will be in the above method, what is common with it.
Thanks!