title: Check for Null in Linq where clause
body: <p>Hi I am working on some old code of ASP.Net MVC controller. There's a Controller Method with 1500 lines of code in it. When I inspected I got to know that there's more than 50+ LINQ queries made to fetch from database according to whether a particular field is null or not.</p>
  
  <p>Just to put it into perspective, here's how this particular method looks like - </p>
  
  <pre><code>public ActionResult MyProductList(string Msg, string ProductName, string CompanyName, string DivisionName, string Type, string Form, string Packing, int page = 1, int pageSize = 20){
  if (CompanyName == null)
              {
                  CompanyName = "";
  
              }
              if (ProductName == null)
              {
                  ProductName = "";
  
              }
              if (DivisionName == null)
              {
                  DivisionName = "";
  
              }
              if (Type == null)
              {
                  Type = "";
  
              }
              if (Form == null)
              {
                  Form = "";
  
              }
              if (Packing == null)
              {
                  Packing = "";
  
              }
      if (CompanyName != "")
              {
                  var query1 = (from mypdtlist in db.MyProductlist
                                join pdt in db.Product on mypdtlist.Product_ID equals pdt.ID
                                where mypdtlist.User_ID.Equals(UserID)
                                select new ViewModal
                                {
                                    ProductName = pdt.ProductName,
                                    CompanyName = pdt.CompanyName,
                                    DivisionName = pdt.DivisionName,
                                    Type = pdt.Type,
                                    Form = pdt.Form,
                                    Packing = pdt.Packing,
                                    MRP = pdt.MRP,
                                    DrugName = pdt.DrugName,
                                    ID = mypdtlist.ID
                                }).Where(x =&gt; x.CompanyName.Contains(CompanyName.ToUpper())).Take(200).ToList();
                  ViewBag.CompanyName = CompanyName;
                  // list = query.Take(100).Where(x =&gt; x.CompanyName.Contains(CompanyName.ToUpper())).ToList();
                  PagedList&lt;ViewModal&gt; model1 = new PagedList&lt;ViewModal&gt;(query1, page, pageSize);
                  CookieManager.AddToViewBag(HttpContext, ViewBag);
                  return View(model1);
              }
              if (ProductName != "")
              {
                  ViewBag.ProductName = ProductName;
                  var query2 = (from mypdtlist in db.MyProductlist
                                join pdt in db.Product on mypdtlist.Product_ID equals pdt.ID
                                where mypdtlist.User_ID.Equals(UserID)
                                select new ViewModal
                                {
                                    ProductName = pdt.ProductName,
                                    CompanyName = pdt.CompanyName,
                                    DivisionName = pdt.DivisionName,
                                    Type = pdt.Type,
                                    Form = pdt.Form,
                                    Packing = pdt.Packing,
                                    MRP = pdt.MRP,
                                    DrugName = pdt.DrugName,
                                    ID = mypdtlist.ID
                                }).Where(x =&gt; x.ProductName.Contains(ProductName.ToUpper())).Take(200).ToList();
                  //list = query.Take(100).Where(x =&gt; x.ProductName.Contains(ProductName.ToUpper())).ToList();
                  PagedList&lt;ViewModal&gt; model1 = new PagedList&lt;ViewModal&gt;(query2, page, pageSize);
                  CookieManager.AddToViewBag(HttpContext, ViewBag);
                  return View(model1);
              }
      //.... After a lot more similar condition checks and LINQ queries
      else if (ProductName != "" &amp;&amp; CompanyName != "" &amp;&amp; DivisionName != "" &amp;&amp; Type != "" &amp;&amp; Form != "" &amp;&amp; Packing != "")
              {
                  ViewBag.ProductName = ProductName;
                  ViewBag.CompanyName = CompanyName;
                  ViewBag.DivisionName = DivisionName;
                  ViewBag.Type = Type;
                  ViewBag.Form = Form;
                  ViewBag.Packing = Packing;
                  var query51 = (from mypdtlist in db.MyProductlist
                                 join pdt in db.Product on mypdtlist.Product_ID equals pdt.ID
                                 where mypdtlist.User_ID.Equals(UserID)
                                 select new ViewModal
                                 {
                                     ProductName = pdt.ProductName,
                                     CompanyName = pdt.CompanyName,
                                     DivisionName = pdt.DivisionName,
                                     Type = pdt.Type,
                                     Form = pdt.Form,
                                     Packing = pdt.Packing,
                                     MRP = pdt.MRP,
                                     DrugName = pdt.DrugName,
                                     ID = mypdtlist.ID
                                 }).Where(x =&gt; x.ProductName.Contains(ProductName.ToUpper()) &amp;&amp; x.CompanyName.Contains(CompanyName.ToUpper()) &amp;&amp; x.DivisionName.Contains(DivisionName.ToUpper()) &amp;&amp; x.Type.Contains(Type.ToUpper()) &amp;&amp; x.Form.Contains(Form.ToUpper()) &amp;&amp; x.Packing.Contains(Packing.ToUpper())).Take(300).ToList();
                  PagedList&lt;ViewModal&gt; model = new PagedList&lt;ViewModal&gt;(query51, page, pageSize);
                  // List&lt;Product&gt; pdt = db.Product.Where(x =&gt; x.ProductName.Contains(ProductName.ToUpper()) &amp;&amp; x.CompanyName.Contains(CompanyName.ToUpper()) &amp;&amp; x.DivisionName.Contains(DivisionName.ToUpper()) &amp;&amp; x.Type.Contains(Type.ToUpper()) &amp;&amp; x.Form.Contains(Form.ToUpper()) &amp;&amp; x.Packing.Contains(Packing.ToUpper())).Take(300).ToList();
                  // PagedList&lt;Product&gt; model = new PagedList&lt;Product&gt;(pdt, page, pageSize);
                  //var list = db.Product.Take(20).ToList();
  
                  return View(model);
              }
  }
  </code></pre>
  
  <p>My apologies for the bad coding, but I didn't do the code. The code is basically using the same method for filtering the products.<br>
  All the parameters to the method are actually optional, thus they all can be null. So the code is basically comparing if the particular parameter is null or not. If it's null, then do not include it into the Where clause.   </p>
  
  <p>Now instead of using 50+ conditions for various combinations of the not null parameters, I'd like to convert this code into a single LINQ call.</p>
  
  <p>So my question is, how can I check whether a particular parameter is null in the Where clause and if it's null, then eliminate its condition from the Where clause? What would be the most optimised and least lines of code to do that?</p>
  
  <p><strong>What did I try?</strong><br>
  I tried to add the null check in the Where clause itself and tried to do something like this - </p>
  
  <pre><code>Where((ProductName!=null &amp;&amp; x.ProductName.Contains(ProductName.ToUpper())) &amp;&amp; (CompanyName!=null &amp;&amp; x.CompanyName.Contains(CompanyName.ToUpper()))
  </code></pre>
  
  <p>But I don't think it's a right condition and it's not giving me any result at all.</p>