This code works fine, but I just don't like all the if else
. If I keep adding field to filter, it's going to get messy.
I am not using EF and cannot on this.
Controller code:
var books = bookRepository.GetDogs();
if (!String.IsNullOrEmpty(searchString))
{
if (!String.IsNullOrWhiteSpace(searchString) && searchGender != String.Empty && searchHand != String.Empty)
{
books = books.Where(b => b.Name.ToUpper().Contains(searchString.ToUpper())
&& b.Gender == searchGender && b.Handedness == searchHand);
}
else if (!String.IsNullOrWhiteSpace(searchGender) && searchString == String.Empty && searchHand == String.Empty)
{
books = books.Where(b => b.Gender == searchGender);
}
else if (String.IsNullOrWhiteSpace(searchGender) && searchString != String.Empty && searchHand == String.Empty)
{
books = books.Where(b => b.Name.ToUpper().Contains(searchString.ToUpper()));
}
}
return View(books);
My view:
<p>Find by Name : @Html.TextBox("searchString", ViewBag.CurrentFilter as string)
@Html.DropDownList("searchGender", new SelectList(ViewBag.BookNames), "-- Select All --")
@Html.DropDownList("searchHand", new SelectList(ViewBag.HandNames), "---Select All--")
var books = from b in bookRepository.GetDogs() select b;
equivalent tobookRepository.GetDogs()
? \$\endgroup\$