我有一个搜索页面,包含4个选项城市,州,年,学校
您在单击提交后将该信息放在文本框中,查询数据库以查找匹配的记录。这有效,但我想知道是否有更有效的做事方式,例如你可以按城市,州或学校和年份进行搜索,但每次我都必须这样做...
if(string school == null && int year == null)
{
// then search for city,state
}
if(string city == null && string state== null)
{
// then search for school, year
}
else
{
// search for city,state,school,year
}
正如您所看到的那样,这只是 4个字段而且搜索每个字段变得乏味,特别是因为我计划在将来添加更多字段。有没有办法可以检查 1 if语句中的所有内容,例如,如果用户离开城市,则将状态设置为空以忽略它。
这是我的城市州搜索的样本
public ActionResult search(string city, string state,string school,int year,int? page)
{
ViewBag.city = city;
ViewBag.state = state;
ViewBag.year = year;
ViewBag.school = school;
int pageSize = 10;
int pageNumber = (page ?? 1);
if (school == null && year ==null)
{
// I would like to put all my variables here and ignore blanks one like
var article = (from x in db.relistings.OrderByDescending(x => x.relistingID)
where x.city == city && x.state == state select x);
return View(article.ToPagedList(pageNumber, pageSize));
}
}