我有一个C#MVC项目。我想在客户端app控制器方法中构建语句以从web.api获取JSON,以便在视图模板中进行分页/排序。排序顺序可以作为变量传递给此代码吗?
var container = new CourseService.Container(uri);
var query = container.Courses.OrderByDescending(c => c.Name).Skip(skip).Take(take).ToList();
答案 0 :(得分:1)
为什么不添加自己的IEnumerable
扩展程序来执行此操作?
public enum SortOrder
{
Ascending,
Descending
}
public static class EnumerableExtensions
{
public static IEnumerable<T> OrderBy<T, TKey>(this IEnumerable<T> input,
Func<T, TKey> keySelector, SortOrder order)
{
switch(order)
{
case SortOrder.Descending:
return input.OrderByDescending(keySelector);
default:
return input.OrderBy(keySelector);
}
}
}
然后:
var output = container.Courses.OrderBy(c => c.Name, SortOrder.Descending);