都市、州、年、学校の 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));
}
}