3

我正在尝试编写一个按列名排序的函数

我在第二种情况下遇到问题=“计数”。如何将转换 IQueryable 转换为 IOrderedQueryable。

谢谢你

 public PagedResults<activity> getList(Expression<Func<activity, bool>> criteria, PagingSpecification paging)
    {
        IQueryable<activity> search = criteria != null? Query.Where(criteria): Query;

        IOrderedQueryable<activity> orderedQuery;

        switch (paging.SortColumnName)
        {=
            case "Name":
                orderedQuery = (paging.SortDirection == SortDirection.Ascending)
                                   ? search.OrderBy(e => e.Person.Name)
                                   : search.OrderByDescending(e => e.Person.Name);
                break;
            case "Count":
                orderedQuery = (paging.SortDirection == SortDirection.Ascending)
                                     ? search.ToList().OrderBy(e => e.GetCount()).AsQueryable<activity>()
                                     : search.ToList().OrderByDescending(e => e.GetCount()).AsQueryable<activity>();
                break;
            default:
                orderedQuery = GetOrderedQueryable(search, paging.SortColumnName, paging.SortDirection);
                break;
        }

        IQueryable<activity> dataQuery = orderedQuery.Skip(paging.StartIndex).Take(paging.PageSize);

        return new PagedResults<activity>(dataQuery.ToList(), search.LongCount());
    }
4

1 回答 1

4

打电话AsQueryable之前OrderBy

orderedQuery = (paging.SortDirection == SortDirection.Ascending)
    ? search.ToList().AsQueryable<activity>().OrderBy(e => e.GetCount())
    : search.ToList().AsQueryable<activity>().OrderByDescending(e => e.GetCount());

OrderBy将 an IEnumerable(like List) 变为 an IOrderedEnumerable,但将 anIQueryable变为IOrderedQueryable.

于 2012-12-06T20:20:35.330 回答