public IQueryable<Property> FindBySearchModel(PropertySearchModel sm) { if (sm.PropertyLocation == null || sm.PropertyLocation.Count == 0) throw new ArgumentException("Suburb must be provided."); if (String.IsNullOrEmpty(sm.RentOrSale)) throw new ArgumentException("Must specify rent or sale."); IQueryable<Property> ps = from p in db.Properties where sm.PropertyLocation.Contains(p.suburb_id) && p.rent_or_sale == sm.RentOrSale.ToLower() && (string.IsNullOrEmpty(p.status) || p.status != PROPERTY_STATUS_ARCHIVED) select p; if (sm.PropertyTypes != null && sm.PropertyTypes.Count(pt => pt != 0) > 0) { ps = ps.Where(p => sm.PropertyTypes.Contains(p.property_type)); } if (sm.NumOfBedroom.HasValue) { ps = ps.Where(p => p.num_of_bedroom >= sm.NumOfBedroom); } if (sm.NumOfBathroom.HasValue) { ps = ps.Where(p => p.num_of_bathroom >= sm.NumOfBathroom); } if (sm.NumOfCarspace.HasValue) { ps = ps.Where(p => p.num_of_carspace >= sm.NumOfCarspace); } if (sm.MinPrice.HasValue) { ps = ps.Where(p => !p.min_price.HasValue || p.min_price >= sm.MinPrice); } if (sm.MaxPrice.HasValue) { ps = ps.Where(p => !p.max_price.HasValue || p.max_price <= sm.MaxPrice); } return ps; }
public ActionResult Search(PropertySearchModel sm, int? page, string sortOrder) { ViewBag.SortParam = sortOrder; SetSearchViewBag(); var models = from m in repository.FindBySearchModel(sm) where m.Language.LanguageName == CultureInfo.CurrentUICulture.Name select m; switch (sortOrder) { case "hp": models = models.OrderByDescending(p => p.min_price); break; case "lp": models = models.OrderBy(p => p.min_price); break; default: models = models.OrderByDescending(p => p.added_on); break; } var listModels = models.AsEnumerable().Select(p => p.ToListModel()); var pageNumber = page ?? 1; return View("Index", listModels.ToPagedList(pageNumber, PAGE_SIZE)); }