toPagedList()顺序by子句中的LINQ顺序不同 [英] toPagedList() order differs from the LINQ order by clause

查看:331
本文介绍了toPagedList()顺序by子句中的LINQ顺序不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示shortURLs的列表给用户一个小应用程序。我使用ASP.NET MVC3,实体框架和Oracle后端。我还使用了PagedList库特洛伊古德( https://github.com/TroyGoode/PagedList

I have a small application that displays a list of shortURLs to a user. I'm using ASP.NET MVC3, Entity Framework, and an Oracle backend. I'm also using the PagedList library by Troy Goode (https://github.com/TroyGoode/PagedList)

我希望用户看到最后进入第一次,类似博客评论的排序方式。要做到这一点,我添加了一个排序依据降条款的LINQ语句:

I want the user to see the very last entry first, similar to how blog comments are ordered. To do this I added an "orderby descending" clause to the LINQ statement:

var links = from l in db.LINKS
            orderby l.ID descending
            select l;

在调试链接对象的要求是有序(降序排列,按主键ID)。

In debug the "links" object is ordered as expected (in descending order, by the primary key "ID").

现在我想通过链接使用.toPagedList()认为,这一清单的方法:

Now I want to pass this list of links to the view 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));

这个伟大的工程,如果我只有3条记录(请参阅pageSize的上图)。然而,当我添加一个创纪录的第四次,我得到意外的行为。因为我有pageSize的设置为3,增加了第四个纪录意味着将有2页。 我期望的顺序是如下:

Page 1:
   ID: 4
   ID: 3
   ID: 2
Page 2:
   ID: 1

那是不是这样的,实际发生是这样的:

Page 1:
   ID: 3
   ID: 2
   ID: 1
Page 2:
   ID: 4

所以我的问题,你到底我做错了吗?为什么PagedList不符合由定义的顺序排序依据L.ID降的LINQ声明前pression?它被莫名其妙我,因为在调试器很显然,链接对象的.toPagedLIst之前正确排序()就可以被使用。

So my question, what the heck am I doing wrong here? Why is PagedList not following the order defined by the "orderby l.ID descending" expression in the LINQ statement? It is baffling to me because in the debugger it is clear that the "links" object is ordered properly before the .toPagedLIst() is used on it.

任何帮助是极大AP preciated!

Any help is greatly appreciated!

推荐答案

您可以尝试:

return View(links.ToPagedList(pageNumber, pageSize, m => m.ID, true));

这是从我的头顶,所以我很抱歉,如果它不能很好地工作...

It's off the top of my head, so I'm sorry if it doesn't work perfectly...

- 哇,才注意到这一个日期......我需要停止拖网解答页面,而不按日期排序

-- Wow, just noticed the date on this one... I need to stop trawling the unanswered pages without sorting by date!

这篇关于toPagedList()顺序by子句中的LINQ顺序不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆