I have a small application that displays a shortURL list for the user. I am using ASP.NET MVC3, Entity Framework and Oracle backend. I also use the Taged Goode PagedList library ( https://github.com/TroyGoode/PagedList )
I want the user to first see the most recent entry, similar to how blog comments are organized. To do this, I added the orderby operator in descending order to the LINQ operator:
var links = from l in db.LINKS orderby l.ID descending select l;
When debugging, the link object is ordered as expected (in descending order using the primary key ID).
Now I want to pass this list of view links using the .toPagedList () method:
int pageSize = 3; int pageNumber = (page ?? 1); // "page" comes from the request, if null set to 1 return View(links.ToPagedList(pageNumber, pageSize));
This works fine if I have only 3 entries (see above "pageSize"). However, when I add the 4th record, I get unexpected behavior. Since I have set pageSize to 3, adding a fourth entry means there will be 2 pages. I expect the order to be as follows:
Page 1: ID: 4 ID: 3 ID: 2 Page 2: ID: 1
This is not what actually happens:
Page 1: ID: 3 ID: 2 ID: 1 Page 2: ID: 4
So my question is, what the hell am I doing wrong here? Why doesn't the PagedList match the order defined by the expression "orderby l.ID in descending order" in the LINQ statement? This puzzles me because in the debugger it is clear that the "links" object is properly ordered before .toPagedLIst () is used on it.
Any help is much appreciated!