【问题标题】:LINQ and PagedList how to orderbyLINQ 和 PagedList 如何排序
【发布时间】:2015-02-17 11:22:42
【问题描述】:

我有下表:

id   serialcode    timestamp
1       0001       01/02/2015
2       0001       02/02/2015
3       0001       03/02/2015
4       0002       03/02/2015

在 linq 中,我获得了分组序列码的最大 id,并按时间戳降序排列结果。 所以我有:

var list = db.mytable.GroupBy(x => x.serialcode).Select(g => g.OrderByDescending(x => x.id).FirstOrDefault()).OrderByDescending(x => x.timestamp);

我得到以下行:

id   serialcode    timestamp
3       0001       03/02/2015
4       0002       03/02/2015

现在我想过滤行,例如,通过序列码并使用 mvc pagedlist 对结果进行分页。 我试过了:

var list = db.mytable.GroupBy(x => x.serialcode).Select(g => g.OrderByDescending(x => x.id).FirstOrDefault());

list = list.Where(x => x.serialcode == myserialcode);

list.OrderByDescending(x => x.timestamp);
....
....
return View(list.ToPagedList(pageNumber, pageSize));

但我有以下错误:“方法 Skip 仅支持 LINQ to Entities 中的排序输入。必须在方法 'Skip' 之前调用方法 'OrderBy'

有什么问题?我已经订购了结果集!?

【问题讨论】:

    标签: c# .net asp.net-mvc linq pagedlist


    【解决方案1】:

    您需要将list.OrderByDescending(x => x.timestamp); 的结果分配给list 对象。

     list = list.OrderByDescending(x => x.timestamp);
    

    OrderByDescending 返回IOrderedEnumerable<TSource>

    查看微软文档here

    【讨论】:

      【解决方案2】:

      你忘记分配

      list = list.OrderByDescending(x => x.timestamp);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        相关资源
        最近更新 更多