我创建了一个扩展方法来共享我的分页方法,扩展方法是:
public static CustomPaginateResult<TEntity> Paginate<TEntity>(this IPagination pagination,
PaginateQueryParameters parameters, IQueryable<TEntity> collection, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy)
where TEntity : class
{
var totalRows = collection.Count();
var totalPages = (int)Math.Ceiling((double)totalRows / parameters.Rows);
var query = collection.Take(parameters.Rows);
var results = orderBy(query)
.Skip(parameters.Page * parameters.Rows)
.ToList();
var result = new CustomPaginateResult<TEntity>()
{
PageSize = parameters.Rows,
TotalRows = totalRows,
TotalPages = totalPages,
CurrentPage = parameters.Page,
Results = results
};
return result;
}
当我调用此方法时,如果 IQueryable collection 不包含任何&#34; include&#34;这工作正常,但是,如果我使用任何&#34; Include&#34;我只能获取第一页的数据,但是,对于以下页面,这不会返回数据。我得到了生成的查询,我发现在子查询中存在顶级clausule,我认为这是问题所在!
好的电话:
var data = _programRepository.Paginate(new PaginateQueryParameters(page, rows),_programRepository.GetAll(), order => order.OrderBy(c => c.Id));
糟糕的电话:
var data = _programRepository.Paginate(new PaginateQueryParameters(page, rows),_programRepository.GetAll("CategoriaPrograma"), order => order.OrderBy(c => c.Id));
一个不良案例的查询示例:
SELECT *
FROM (SELECT TOP (50) *
FROM [dbo].[Sesion] AS [c] ) AS [Limit1]
LEFT OUTER JOIN [dbo].[Modulo] AS [Extent2] ON [Limit1].[ModuloId] = [Extent2].[Id]
ORDER BY [Limit1].[Id] ASC
任何人都可以帮助我?
答案 0 :(得分:0)
我希望Take在跳过之后。在提示bij @Hogan之后编辑。
你试过这个:
var results = orderBy(collection)
.Skip(parameters.Page * parameters.Rows)
.Take(parameters.Rows)
.ToList();