【发布时间】:2013-06-22 20:49:09
【问题描述】:
我正在尝试对数据集实施过滤/排序/分页。我想按搜索字符串过滤,然后应用排序,然后选择该集合的子集作为页面。
代码如下:
IEnumerable<Manufacturer> manufacturers;
if (!String.IsNullOrEmpty(genericSearch))
{
manufacturers = db.Manufacturers.Where(l => l.Name.Contains(genericSearch));
}
manufacturers = manufacturers.OrderBy(sortColName, sortDir, true); // this won't build. it would
// build if i put 'db.Manufacturers' before the .OrderBy but then i lose my filter. it would
// also build if i used 'l => l.Name' as the OrderBy parameter but i only have the column name
//as a string from the client.
manufacturers.Skip(displayStart).Take(displayLength).ToList().ForEach(rec => aaData.Add
(rec.PropertiesToList())); // this is paging where i can use ToList()
如何做到这一点以允许将列名作为字符串进行排序?
【问题讨论】:
-
Get Dynamic OrderBy in Linq 的可能重复项
-
谢谢。甚至希望有一个更简单的解决方案或替代方法。
-
使用 Skeet 的解决方案 -- 它比其他任何一个都快。
-
好的,我已经成功运行了。我还在 Jon Skeet 的解决方案中添加了一个“OrderByPropertyDescending”方法。这可以安全使用吗?在他的代码中,他评论了“TODO:大量验证:)”
标签: c# linq pagination