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

asp.net-mvc, c#, linq

Solution

Because you're providing `Func<...>` delegate, `IEnumerable.OrderBy` extension method is chosen. Change your 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 make `IQueryable.OrderBy()` method to be choose instead of `IEnumerable.OrderBy()` when you actually call `OrderBy()` later.

Problem

Trying to add an `orderby` statement to my generic repository method and getting the below error. Not sure why as it seems I am able to add a .OrderBy to an 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); } } ```

Original source