I am trying to implement a search box with a number of filtering option I would like to count how many possible conditions that I would have. for example I have two search boxes with options to search by title or search by location or search by both and also I have two options display results based on the last 24 hour posted results or within 3 days. Some of the conditions for instance could be that search location is empty where the rest is not and another example would be that the option 24 hours is on where the rest is not. the attempted solution for this is as below but what I am looking for is the accurate way of counting all of these conditions.
public ActionResult SearchResult(int? page, string searchTitle = "" , string searchLocation = "", string last24="",string last3Days="")
{
setUpApi(searchTitle, searchLocation);
var result = new List<AllJobModel>();
DateTime now = DateTime.Now;
var time = now.AddHours(-24).ToString("dd/MM/yyyy").ToString();
var time2 = now.AddHours(-48).ToString("dd/MM/yyyy").ToString();
var time3 = now.AddHours(-72).ToString("dd/MM/yyyy").ToString();
var timesNow = now.ToString("dd/MM/yyyy").ToString();
if ((!string.IsNullOrEmpty(searchTitle) || !string.IsNullOrEmpty(searchLocation)) && (!string.IsNullOrEmpty(last24) || !string.IsNullOrEmpty(last3Days)))
{
setUpApi(searchTitle, searchLocation);
if(!string.IsNullOrEmpty(last24))
{
time = now.AddHours(-24).ToString("dd/MM/yyyy").ToString();
result = db.AllJobModel.Where(a => a.JobTitle.Contains(searchTitle) && a.locationName.Contains(searchLocation) && a.PostedDate == timesNow ||
a.PostedDate.Equals(time)).ToList();
}
else if(!string.IsNullOrEmpty(last3Days))
{
time2 = now.AddHours(-48).ToString("dd/MM/yyyy").ToString();
result = db.AllJobModel.Where(a => a.JobTitle.Contains(searchTitle) && a.locationName.Contains(searchLocation) && a.PostedDate == timesNow ||
a.PostedDate.Equals(time3) || a.PostedDate == time2 || a.PostedDate == time).ToList();
}
}else if(!string.IsNullOrEmpty(searchTitle) || !string.IsNullOrEmpty(searchLocation) && (string.IsNullOrEmpty(last24) && string.IsNullOrWhiteSpace(last3Days)))
{
result = db.AllJobModel.Where(a => a.JobTitle.Contains(searchTitle) && a.locationName.Contains(searchLocation)).ToList();
}
else if ((string.IsNullOrEmpty(searchTitle) || string.IsNullOrEmpty(searchLocation)) && (!string.IsNullOrEmpty(last24) || !string.IsNullOrEmpty(last3Days)))
{
if(!string.IsNullOrEmpty(last24) && !string.IsNullOrEmpty(searchTitle))
{
result = db.AllJobModel.Where(a => a.PostedDate.Equals(time) && a.JobTitle == searchTitle).ToList();
}else if(!string.IsNullOrEmpty(last24) && !string.IsNullOrEmpty(searchLocation))
{
result = db.AllJobModel.Where(a => a.PostedDate.Equals(time) && a.locationName == searchLocation).ToList();
}
else if(!string.IsNullOrEmpty(last3Days) && !string.IsNullOrEmpty(searchTitle))
{
result = db.AllJobModel.Where(a => a.JobTitle == searchTitle && a.PostedDate.Equals(time) || a.PostedDate == time2 || a.PostedDate == time3 || a.PostedDate == timesNow).ToList();
}else if(!string.IsNullOrEmpty(last3Days) && !string.IsNullOrEmpty(searchLocation))
{
result = db.AllJobModel.Where(a => a.locationName == searchLocation && a.PostedDate.Equals(time) || a.PostedDate == time2 || a.PostedDate == time3 || a.PostedDate == timesNow).ToList();
}else if(!string.IsNullOrEmpty(last3Days))
{
result = db.AllJobModel.Where(a => a.PostedDate.Equals(time) || a.PostedDate == time2 || a.PostedDate == time3 || a.PostedDate == timesNow).ToList();
}else if(!string.IsNullOrEmpty(last24))
{
result = db.AllJobModel.Where(a => a.PostedDate == time).ToList();
}
}
else
{
result = (from app in db.AllJobModel select app).ToList();
}
return View(result.ToList().ToPagedList(page ?? 1, 5));
}
Aucun commentaire:
Enregistrer un commentaire