Cha*_*son 7 c# linq asp.net-mvc
尝试将orderby语句添加到我的通用存储库方法并获得以下错误.不知道为什么我似乎能够在其他情况下将.OrderBy添加到IQueryable.
我错过了什么?
得到错误:
无法将类型'System.Linq.IOrderedEnumerable'隐式转换为'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,
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*zek 14
因为您提供Func<...>委托,IEnumerable.OrderBy所以选择了扩展方法.将方法参数更改为Expression<Func<...>>:
public virtual IQueryable<TEntity> GetAll(
int page, int pageSize,
Expression<Func<TEntity, object>> orderbyascending = null,
Expression<Func<TEntity, object>> orderbydescending = null)
Run Code Online (Sandbox Code Playgroud)
它将使IQueryable.OrderBy()方法被选择,而不是IEnumerable.OrderBy()在你OrderBy()以后实际调用时.
| 归档时间: |
|
| 查看次数: |
2150 次 |
| 最近记录: |