Cannot convert Linq.IOrderedEnumerable <T> to Linq.IQueryable <T>

Trying to add an orderby to my generic repository method and get the following error. Not sure why, it seems to me, I can add .OrderBy to IQueryable in other cases.

What am I missing?

Getting error:

Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'System.Linq.IQueryable'

Code snippet (some parts removed):

 public class QuickbooksRespository<TEntity> where TEntity : class, Intuit.Ipp.Data.IEntity, new() { public virtual IQueryable<TEntity> GetAll( int page, int pageSize, Func<TEntity, object> orderbyascending = null, Func<TEntity, object> orderbydescending = null) { int skip = Math.Max(pageSize * (page - 1), 0); IQueryable<TEntity> results = _qbQueryService .Select(all => all); if (orderbyascending != null) { results = results.OrderBy(orderbyascending); } if (orderbydescending != null) { results = results.OrderByDescending(orderbydescending); } return results .Skip(skip) .Take(pageSize); } } 
+6
source share
1 answer

Since you are providing the Func<...> delegate, the IEnumerable.OrderBy extension method is selected. Change the method parameters to Expression<Func<...>> :

 public virtual IQueryable<TEntity> GetAll( int page, int pageSize, Expression<Func<TEntity, object>> orderbyascending = null, Expression<Func<TEntity, object>> orderbydescending = null) 

It will select the IQueryable.OrderBy() method instead of IEnumerable.OrderBy() when you actually call OrderBy() later.

+12
source

All Articles