2012-12-06 159 views
3

我想寫一個函數,按列名排序C#LINQ to Entities不認識的方法

我在第二種情況case =「Count」有問題。如何將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()); 
    } 
+0

你爲什麼使用IOrderedQueryable?它與IEnumerable或IQueryable不一樣嗎? – lante

回答

4

呼叫AsQueryableOrderBy前:

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

OrderBy變成一個IEnumerable(如List)爲IOrderedEnumerable,但原來的IQueryableIOrderedQueryable

相關問題