Exemplo n.º 1
0
        public PagedSearchList <Product> Search(IProductSearchCriteria productSearch)
        {
            var productsQuery = _northwindEntityDataModel.Products.AsQueryable();

            if (productSearch.Id.HasValue)
            {
                productsQuery = productsQuery.Where(x => x.ProductID == productSearch.Id.Value);
            }

            if (productSearch.SelectedCategories != null && productSearch.SelectedCategories.Any())
            {
                productsQuery = productsQuery.Where(x => productSearch.SelectedCategories.Contains(x.CategoryID.ToString()));
            }

            if (productSearch.SelectedSuppliers != null && productSearch.SelectedSuppliers.Any())
            {
                productsQuery = productsQuery.Where(x => productSearch.SelectedSuppliers.Contains(x.SupplierID.ToString()));
            }

            if (!string.IsNullOrEmpty(productSearch.SearchTerm))
            {
                productsQuery = productsQuery.Where(x => x.ProductName.ToLower().Contains(productSearch.SearchTerm.ToLower()));
            }

            if (productSearch.IsInStock)
            {
                productsQuery = productsQuery.Where(x => x.UnitsInStock.HasValue && x.UnitsInStock.Value > 0);
            }

            if (productSearch.IsContinued)
            {
                productsQuery = productsQuery.Where(x => !x.Discontinued);
            }

            if (productSearch.MinUnitPrice.HasValue)
            {
                productsQuery = productsQuery.Where(x => x.UnitPrice.HasValue &&
                                                    x.UnitPrice.Value >= productSearch.MinUnitPrice);
            }

            if (productSearch.MaxUnitPrice.HasValue)
            {
                productsQuery = productsQuery.Where(x => x.UnitPrice.HasValue &&
                                                    x.UnitPrice.Value <= productSearch.MaxUnitPrice);
            }

            if (productSearch.PagedBase == null)
            {
                productSearch.PagedBase           = new PagedBase();
                productSearch.PagedBase.PageIndex = 1;
                productSearch.PagedBase.PageSize  = 10;
            }

            var pagedProducts = productsQuery.ToPage(productSearch.PagedBase);

            return(pagedProducts);
        }
Exemplo n.º 2
0
        public ProductsCollection Search(IProductSearchCriteria productSearchCriteria)
        {
            var result = new ProductsCollection();

            foreach (var item in _list)
            {
                var product = (Product)item;
                if (productSearchCriteria.isSatisfiedBy(product))
                {
                    result.Add(product);
                }
            }
            return(result);
        }
 public InverseCriteria(IProductSearchCriteria searchCriteria)
 {
     _searchCriteria = searchCriteria;
 }