title: ASP.NET MVC 4 MvcMovie Tutorial Filter query by multiple search criteria
body: <p>I was going through the MvcMovie Tutorial and I was trying to modify the SearchIndex() call in the MoviesController to search by multiple criteria. The tutorial shows you how to search by title and genre using the following code:</p>
  
  <pre><code>   var movies = from m in db.Movies 
               select m; 
  
     if (!String.IsNullOrEmpty(searchString)) 
     { 
         movies = movies.Where(s =&gt; s.Title.Contains(searchString)); 
     } 
  
     if (string.IsNullOrEmpty(movieGenre)) 
         return View(movies); 
     else 
     { 
         return View(movies.Where(x =&gt; x.Genre == movieGenre)); 
     }
  </code></pre>
  
  <p>So if I read this right they return all movies then filter on Genre. That would be fine for a dual case but when you add in a 3rd or 4th search criteria you would then increase the amount of if statement calls by a factor of 2 (to the n) - 1 cases. This would get very unruly if for say you had 10 or so columns to search by. </p>
  
  <p>I tried to use something a little more straight forward like:</p>
  
  <pre><code>var movieQry = from m in db.Movies
                  where ((m.Title !=null &amp;&amp; m.Title == searchString) ||
                         (m.Rating != null &amp;&amp; m.Rating == movieRating) ||
                         (m.Genre != null &amp;&amp; m.Genre == movieGenre))
                  select m;
  </code></pre>
  
  <p>but that would not return anything when you first visit the page and only works for a single filter (if I select the Genre - I get good results but not when I select Genre and Rating) and I get nothing returned when I enter a search term for Title.</p>
  
  <p>Is there an easier way to accomplish? (At some point I will need to be able to search on dozens of filters for a project I will be writing... and I have other questions about how to search database for entries but for not this will get me moving in the right direction).</p>