C# Reverse IQueryable
我正在从另一个方法(
1 2 3 4 5 6 7 8 9 10 11 12 | public async Task<PagedList<Item>> GetItems(FilterParameters filterParameters) { var items = QueryItems(filterParameters) if (filterParams.Descending) { //do something to reverse the order of the IQueryable } return await PagedList<Item> .CreateAsync(items, filterParams.PageNumber, filterParams.PageSize) } |
我不能执行
当我尝试执行
System.NotImplementedException: Remotion.Linq.Clauses.ResultOperators.ReverseResultOperator
主要是因为我很懒惰,我不想使用
所以…你如何颠倒iqueryable的顺序?
编辑-包含pagedlist.createasync(…)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class PagedList<T> : List<T> { public int CurrentPage { get; set; } public int TotalPages { get; set; } public int PageSize { get; set; } public int TotalCount { get; set; } public PagedList(List<T> items, int count, int pageNumber, int pageSize) { TotalCount = count; PageSize = pageSize; CurrentPage = pageNumber; TotalPages = (int) Math.Ceiling(count / (double) pageSize); this.AddRange(items); } public static async Task<PagedList<T>> CreateAsync(IQueryable<T> source, int pageNumber, int pageSize) { var count = await source.CountAsync(); var items = await source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync(); return new PagedList<T>(items, count, pageNumber, pageSize); } } |
正如下面的注释所指出的,您几乎肯定希望颠倒整个查询的顺序,以便最后一页成为第一页,反之亦然。为此,您别无选择,只能使用
1 | Expression<Func<Item,int>> orderBy = o => o.Id; |
注意:上面的
然后:
1 2 3 | items = filterParams.Descending ? items.OrderByDescending(orderBy) : items.OrderBy(orderBy); |
写一个