protected static IQueryable <TEntity> Sort <TEntity>(IQueryable <TEntity> query, PageDto pageDto) { if (!string.IsNullOrWhiteSpace(pageDto.Sort)) { var memberExpr = ExpressionBuilder.MemberExpression <TEntity>(pageDto.Sort); query = pageDto.Ascending ? query.OrderBy(memberExpr) : query.OrderByDescending(memberExpr); } return(query); }
protected static IQueryable <TEntity> FilterFlat <TEntity>(IQueryable <TEntity> query, PageDto pageDto) { if (!string.IsNullOrWhiteSpace(pageDto.Filter) && pageDto.FilterFields.Any()) { var whereExpr = ExpressionBuilder.WhereExpression <TEntity>(pageDto.FilterFields.Split(','), pageDto.Filter); query = query.Where(whereExpr); } return(query); }
protected static IQueryable <TEntity> Paginate <TEntity>(IQueryable <TEntity> query, PageDto pageDto) { return(query.Skip((pageDto.Page - 1) * pageDto.PageSize).Take(pageDto.PageSize)); }
public static PagedResult FromAnonymous <TEntity>(IQueryable <TEntity> query, PageDto pageDto) { var pagedResult = new PagedResult <TEntity>(); //apply filtering query = FilterFlat(query, pageDto); //calc total count pagedResult.TotalCount = query.Count(); //apply sorting query = Sort(query, pageDto); //apply paging query = Paginate(query, pageDto); //materialize pagedResult.Result = query.ToList(); pagedResult.Page = pageDto.Page; pagedResult.PageSize = pageDto.PageSize; return(pagedResult); }