Example #1
0
        public ActionResult IndexAjax(CarFilterModel filter)
        {
            var carQuery = this._dbContext.Cars.Include(c => c.Brand).Include(c => c.Country).Include(c => c.Reviewer).AsQueryable();

            //Primjer iterativnog građenja upita - dodaje se "where clause" samo u slučaju da je parametar doista proslijeđen.
            //To rezultira optimalnijim stablom izraza koje se kvalitetnije potencijalno prevodi u SQL
            if (!string.IsNullOrWhiteSpace(filter.Brand))
            {
                carQuery = carQuery.Where(p => p.BrandID != null && p.Brand.Name.Contains(filter.Brand.ToLower()));
            }

            if (!string.IsNullOrWhiteSpace(filter.Model))
            {
                carQuery = carQuery.Where(p => p.Model.Contains(filter.Model.ToLower()));
            }

            if (!string.IsNullOrWhiteSpace(filter.Engine))
            {
                carQuery = carQuery.Where(p => p.Engine.Contains(filter.Engine.ToLower()));
            }

            if (!string.IsNullOrWhiteSpace(filter.Country))
            {
                carQuery = carQuery.Where(p => p.CountryID != null && p.Country.Name.Contains(filter.Country.ToLower()));
            }

            if (!string.IsNullOrWhiteSpace(filter.Reviewer))
            {
                carQuery = carQuery.Where(p => p.ReviewerID != null && p.Reviewer.FullName.Contains(filter.Reviewer.ToLower()));
            }

            var model = carQuery.ToList();

            return(PartialView("_IndexTable", model));
        }
        public ActionResult IndexAjax(CarFilterModel filter)
        {
            var carList = this._dbContext.Cars
                          .Include(p => p.Maker)
                          .Include(p => p.Owner)
                          .ToList();

            carList = carList
                      .Where(p => string.IsNullOrWhiteSpace(filter.Name) || p.ModelName.ToLower().Contains(filter.Name.ToLower()))
                      .Where(p => string.IsNullOrWhiteSpace(filter.Color) || p.Color.ToLower().Contains(filter.Color.ToLower()))
                      .Where(p => string.IsNullOrWhiteSpace(filter.Maker) || (p.Maker != null && p.Maker.Name.ToLower().Contains(filter.Maker.ToLower())))
                      .Where(p => string.IsNullOrWhiteSpace(filter.Year) || p.ManufactureDate.Year.ToString().ToLower().Contains(filter.Year.ToLower()))
                      .Where(p => string.IsNullOrWhiteSpace(filter.Owner) || (p.Owner != null && p.Owner.FullName.ToLower().Contains(filter.Owner.ToLower())))
                      .ToList();

            var model = carList.ToList();

            return(PartialView("_IndexTable", model));
        }