[英]LINQ and PagedList how to orderby
我有下表:
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));
但是我有以下错误:“仅对LINQ到实体中的排序输入支持“跳过”方法。必须在方法“跳过”之前调用方法“ OrderBy”
有什么问题? 我已订购结果集!?
您需要分配list.OrderByDescending(x => x.timestamp);
list
对象。
list = list.OrderByDescending(x => x.timestamp);
OrderByDescending
返回IOrderedEnumerable<TSource>
在此处查看Microsoft文档。
你忘了分配
list = list.OrderByDescending(x => x.timestamp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.