private static Expression <Func <Product, bool> > GetProductFilter(ProductPagedDto requestFilter) { var predicate = PredicateBuilder.New <Product>(true); if (!string.IsNullOrEmpty(requestFilter.Name)) { predicate = predicate.And(a => a.Name.ToLower() == requestFilter.Name.ToLower()); } if (!string.IsNullOrEmpty(requestFilter.Color)) { predicate = predicate.And(a => a.Color == requestFilter.Color); } if (!string.IsNullOrEmpty(requestFilter.Reference)) { predicate = predicate.And(a => a.Reference == requestFilter.Reference); } return(predicate); }
public async Task <GenericCollectionResponse <IList <ProductDto> > > GetPaged(ProductPagedDto dto) { var response = new GenericCollectionResponse <IList <ProductDto> >(); Func <IQueryable <Product>, IOrderedQueryable <Product> > order = product => product.OrderBy(x => x.Name); if (!string.IsNullOrEmpty(dto.SortParameter)) { order = GetProductOrder(dto.SortParameter, dto.SortOrder); } var filter = GetProductFilter(dto); var totalRecords = await _unitOfWork.ProductRepository.GetCountAsync(filter); response.TotalRecords = totalRecords; var products = await _unitOfWork.ProductRepository.GetAsync(filter, order, "Categories,Size", dto.StartRowIndex, dto.MaximumRows); var result = _mapper.Map <IList <Product>, IList <ProductDto> >(products.ToList()); response.Result = result; return(await Task.FromResult(response)); }