LINQ和PagedList如何排序

LINQ and PagedList how to orderby

我有以下 table:

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 中的排序输入。方法 'OrderBy' 必须在方法 'Skip'

之前调用

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

你忘记赋值了

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

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

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

OrderByDescending returns IOrderedEnumerable<TSource>

查看 Microsoft 文档 here