[英]LINQ and PagedList how to orderby
i have the following table: 我有下表:
id serialcode timestamp
1 0001 01/02/2015
2 0001 02/02/2015
3 0001 03/02/2015
4 0002 03/02/2015
In linq i have got the max id for a grouped serialcode and ordered the result by descending timestamp. 在linq中,我得到了分组的序列码的最大ID,并按时间戳记对结果进行排序。 So i have:
所以我有:
var list = db.mytable.GroupBy(x => x.serialcode).Select(g => g.OrderByDescending(x => x.id).FirstOrDefault()).OrderByDescending(x => x.timestamp);
I get the following rows: 我得到以下行:
id serialcode timestamp
3 0001 03/02/2015
4 0002 03/02/2015
Now i want filter the rows, for example, by serialcode and use the mvc pagedlist to paging the results. 现在,我想按行代码过滤行,并使用mvc pagedlist对结果进行分页。 I have tried:
我努力了:
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));
But i have the following error: "The method Skip is only supported for sorted input in LINQ to Entities. The methos 'OrderBy' must be called before the methos 'Skip' 但是我有以下错误:“仅对LINQ到实体中的排序输入支持“跳过”方法。必须在方法“跳过”之前调用方法“ OrderBy”
What's the problem? 有什么问题? I have ordered the result set!?
我已订购结果集!?
you need to assign result of list.OrderByDescending(x => x.timestamp);
您需要分配
list.OrderByDescending(x => x.timestamp);
to list
object. list
对象。
list = list.OrderByDescending(x => x.timestamp);
OrderByDescending
returns IOrderedEnumerable<TSource>
OrderByDescending
返回IOrderedEnumerable<TSource>
你忘了分配
list = list.OrderByDescending(x => x.timestamp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.