加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 无法将Linq.IOrderedEnumerable转换为Linq.IQueryable

发布时间:2020-12-15 18:09:46 所属栏目:百科 来源:网络整理
导读:尝试将orderby语句添加到我的通用存储库方法并获取以下错误.不知道为什么我似乎能够在其他情况下将.OrderBy添加到IQueryable. 我错过了什么? 得到错误: Cannot implicitly convert type ‘System.Linq.IOrderedEnumerable’ to ‘System.Linq.IQueryable’
尝试将orderby语句添加到我的通用存储库方法并获取以下错误.不知道为什么我似乎能够在其他情况下将.OrderBy添加到IQueryable.

我错过了什么?

得到错误:

Cannot implicitly convert type ‘System.Linq.IOrderedEnumerable’ to ‘System.Linq.IQueryable’

代码段(删除了一些部分):

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,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);
    }
}

解决方法

因为你提供Func< ...>委托,IEnumerable.OrderBy选择扩展方法.将方法参数更改为Expression< Func< ...>>:
public virtual IQueryable<TEntity> GetAll(
    int page,Expression<Func<TEntity,object>> orderbyascending = null,object>> orderbydescending = null)

当你实际上稍后调用OrderBy()时,它将使IQueryable.OrderBy()方法被选择而不是IEnumerable.OrderBy().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读