I am trying to use a single textbox for a search in two model fields: In a textbox I write some letters or a word and click on Search button, so must search in CommonName and in SciName fields (both in the same model) . Here is an example of the controller:
public ActionResult Index(string SearchParam)
{
var search = from m in db.Table1
select m;
if (!String.IsNullOrEmpty(SearchParam))
{
search= search.Where(s => s.CommonName.Contains(SearchParam) ||
s.SciName.Contains(SearchParam));
}
return View(search)
}
CommonName and SciName are from the same model.
The view:
@using (Html.BeginForm())
{
<table>
<th>
Serch for Name: @Html.TextBox("SearchParam")
<input type="submit" value="Search" /></th>
</table>
}
but Visual Studio display a message saying that "||" operator can't be used with lambda expressions. Somebody knows how to do this search??? Thanks!!!
----- UPDATED----- NO MORE LAMBDA EXPRESSION ERROR -----------
I checked the code again, and now the error is not displayed... .. but the search is made only with the first part of:
search= search.Where(s => s.CommonName.Contains(SearchParam) ||
s.SciName.Contains(SearchParam));
it is only made with s => s.CommonName.Contains(SearchParam)
and the second condition after ||
is just ignored