0

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

4

1 回答 1

0

我刚刚发现了问题。这是代码其他部分的身份验证问题。在这里我留下完整的代码:

public ActionResult Index(string SearchParam)
        {

            var search1= (dynamic)null;

            var search= from m in db.Table1
                          select m;


            int selec = 0;

            if (User.Identity.IsAuthenticated)
            {

                if (!String.IsNullOrEmpty(SearchParam))
                {
                    search= search.Where(s => s.CommonName.Contains(SearchParam) || s.SciName.Contains(SearchParam));
                    selec = 1;
                }

                search1= (from d in db.Table1
                            select d).OrderBy(p => p.CommonName);


            }
            else
            {
                if (!String.IsNullOrEmpty(SearchParam))
                {
                    search= search.Where(s => s.CommonName.Contains(SearchParam)||s.SciName.Contains(SearchParam)).Where(s => s.State.Equals(true));
                    selec = 1;
                }

                search1= (from d in db.Table1
                            where d.State== true
                            select d).OrderBy(p => p.CommonName);
            }

            if (selec == 0)
            {
                return View(search1);
            }
            return View(search);

        }
于 2013-10-23T15:29:44.533 回答