public IActionResult GetAllByPage(PaginatorModel page) { if (page == null) { page = new PaginatorModel(); } else { //if (page.PageIndex == 0) page.PageIndex = 1; if (page.PageSize == 0) { page.PageIndex = 10; } } var totalCount = _service.Queryable().Count(); var sort = PaginatorHelp.GetSortString(page); IQueryable <TEntity> queryable = null; //SQL Server 2008以上版本的分頁寫法 //if (!string.IsNullOrEmpty(sort)) // queryable = _service.Queryable().OrderBy(sort); //else // queryable = _service.Queryable(); //SQL Server 2008以下版本的分頁寫法 // queryable = _service.Queryable().OrderBy(sort).Skip((page.PageIndex-1) * page.PageSize).Take(page.PageSize); queryable = _service.Queryable().OrderBy(sort).Skip((page.PageIndex) * page.PageSize).Take(page.PageSize); var result = _mapper.Map <List <T> >(queryable); var model = new { total_count = totalCount, data = result }; return(Success(model)); }
private IQueryable <TEntity> ApplySorting(IQueryable <TEntity> query, PaginatorModel page) { return(query.OrderBy(PaginatorHelp.GetSortString(page))); }