title: Filter/Search using Multiple Fields - ASP.NET MVC
body: <p>I am using <strong>ASP.NET MVC</strong> with <strong>EF 6</strong>.</p>
  
  <p>I have a stock page which shows all the information on stock items. Now I want to filter records too.</p>
  
  <p>In picture below I have 3 options. I might filter by each option, one at a time or by combination of two or with all three. </p>
  
  <p>I was thinking of writing linq query for each and every options selected. But  this wouldn't be possible if filter option increases.Is there is any better way to this. </p>
  
  <p>Thanks!</p>
  
  <p><a href="https://i.stack.imgur.com/S9NNZ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/S9NNZ.png" alt="enter image description here"></a></p>
  
  <p>This is what I did in my controller.(currently dropdown has two options, excluding : " -- select one -- ")</p>
  
  <pre><code>public ActionResult StockLevel(string option, string batch, string name)
  {
      if (option != "0" &amp;&amp; batch == "" &amp;&amp; name == "")
      {
          if(option == "BelowMin")
          {
              List&lt;Stock&gt; stk = (from s in db.Stocks
                                 where s.Qty &lt; s.Item.AlertQty
                                 select s).ToList();
              return View(stk);
          }
          else
          {
              List&lt;Stock&gt; stk = (from s in db.Stocks
                                 where s.Qty == s.InitialQty
                                 select s).ToList();
              return View(stk);
          }
      }
      if (option == "0" &amp;&amp; batch != "" &amp;&amp; name == "")
      {
          List&lt;Stock&gt; stk = (from s in db.Stocks
                             where s.BatchNo == batch
                             select s).ToList();
          return View(stk);
      }
      if (option == "0" &amp;&amp; batch == "" &amp;&amp; name != "")
      {
          List&lt;Stock&gt; stk = (from s in db.Stocks
                             where s.Item.Name.StartsWith(""+name+"")
                             select s).ToList();
          return View(stk);
      }
      return View(db.Stocks.ToList());
  }
  </code></pre>