public static T[] ApplySortingPaging <T>(this IOrderedQueryable <T> query, SortingPagingDto sortingPaging) { var firstPass = true; foreach (var sortOrder in sortingPaging.SortOrders) { if (firstPass) { firstPass = false; query = sortOrder.ColumnOrder == SortOrderDto.SortOrder.Ascending ? query.OrderBy(sortOrder.ColumnName) : query.OrderByDescending(sortOrder.ColumnName); } else { query = sortOrder.ColumnOrder == SortOrderDto.SortOrder.Ascending ? query.ThenBy(sortOrder.ColumnName) : query.ThenByDescending(sortOrder.ColumnName); } } var result = query.Skip((sortingPaging.PageNumber - 1) * sortingPaging.NumberRecords).Take(sortingPaging.NumberRecords).ToArray(); return(result); }
public CustomerDto[] SelectCustomer(CustomerSearchCriteriaDto searchCriteria, SortingPagingDto sortingPaging) { using (var context = new MyContext()) { var predicate = ExpressionExtensions.BuildPredicate <Customer, CustomerSearchCriteriaDto>(searchCriteria); var query = context.Customers.AsExpandable().Where(predicate) as IOrderedQueryable <Customer>; var dbCustomers = query.ApplySortingPaging(sortingPaging); var customerDtos = dbCustomers.ToDto(); return(customerDtos); } }