C# 无法转换Linq.IORDerenumerable<;T>;致Linq.IQueryable<;T>;
试图将C# 无法转换Linq.IORDerenumerable<;T>;致Linq.IQueryable<;T>;,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,试图将orderby语句添加到我的通用存储库方法中,并出现以下错误。不确定为什么,因为在其他情况下,我似乎能够将.OrderBy添加到IQueryable 我错过了什么 获取错误: 无法将类型“System.Linq.iorderenumerable”隐式转换为“System.Linq.IQueryable” 代码段(已删除某些部分): 公共类QuickbooksRespository 其中TEntity:class,Intuit.Ipp.Data.IEntity,new() { 公共虚拟IQu
orderby
语句添加到我的通用存储库方法中,并出现以下错误。不确定为什么,因为在其他情况下,我似乎能够将.OrderBy添加到IQueryable
我错过了什么
获取错误:
无法将类型“System.Linq.iorderenumerable”隐式转换为“System.Linq.IQueryable”
代码段(已删除某些部分):
公共类QuickbooksRespository
其中TEntity:class,Intuit.Ipp.Data.IEntity,new()
{
公共虚拟IQueryable GetAll(
int page,int pageSize,
Func orderbyascending=null,
Func orderbydescending=null)
{
int skip=Math.Max(pageSize*(第1页),0);
IQueryable结果=\u qbQueryService
.选择(全部=>全部);
if(orderbyascending!=null)
{
结果=results.OrderBy(orderbyascending);
}
if(orderbydescending!=null)
{
结果=结果.OrderByDescending(OrderByDescending);
}
返回结果
.Skip(Skip)
。取(页面大小);
}
}
因为您提供了Func
委托,所以选择了IEnumerable.OrderBy
扩展方法。将方法参数更改为表达式
:
公共虚拟IQueryable GetAll(
int page,int pageSize,
表达式orderbyascending=null,
表达式orderbydescending=null)
当您稍后实际调用
OrderBy()
时,将选择IQueryable.OrderBy()
方法,而不是IEnumerable.OrderBy()
。谢谢,谢谢!他在拔我的头发。我对这样的表达式和函数还没有太多的经验。
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);
}
}
public virtual IQueryable<TEntity> GetAll(
int page, int pageSize,
Expression<Func<TEntity, object>> orderbyascending = null,
Expression<Func<TEntity, object>> orderbydescending = null)