title: How to filter IEnumerable based on an entity input parameter
body: <p>I'm using now Entity framework- but it's a problem "shared" between all ORM's and even IEnumerable.</p>
  
  <p>Let's say I have a method in MVC looks like this:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Foo(FooModel model)
  {
      var context = new Context(); -- The EF session
      var data = context.Foo.Where(???).ToList();
      return View(data);
  }
  </code></pre>
  
  <p>I want  to query the context based on the input parameter like:</p>
  
  <pre><code>var data = context.Foo.Where(x =&gt; x.Date == model.Date &amp;&amp;
                               x.Name == model.Name &amp;&amp;
                               x.ItemCode = model.ItemCode).ToList();
  </code></pre>
  
  <p>But it's more complicated than that, because if one of the parameters above(<code>Date</code>\ <code>Name</code>\ <code>ItemCode</code>) is null I don't want to include it inside the query.<br>
  If I hard code it can looks similar to this:</p>
  
  <pre><code>var query =  context.Foo;
  
  if (model.Date != null)
      query =query.Where(x =&gt; x.Date == model.Date);
  
  if (model.ItemCode != null)
      query =query.Where(x =&gt; x.ItemCode == model.ItemCode);
  ...
  </code></pre>
  
  <p>There must be a simpler way than this.<br>
  I need a way to generate an expression of the type <code>Expression&lt;T, bool&gt;</code> to be used in the Where method.</p>
  
  <pre><code>[HttpPost]
  public ActionResult Foo(FooModel model)
  {
      var context = new Context(); -- The EF session
      var data = context.Foo.Where(THE_EXPRESSION).ToList();
      return View(data);
  }
  </code></pre>
  
  <p>Is there a built-in way to build that expression? Is there a package in nuget that does it?</p>
  
  <hr>
  
  <p><strong>Update:</strong> There could be more than 30 properites in the model-entity; writing 30 times the Where for each query can be a pain in the neck:</p>
  
  <pre><code>.Where(model.Date != null, x =&gt; x.Date == model.Date)
  .Where(model.Name != null, x =&gt; x.Name == model.Name)
  .Where(model.ItemCode != null, x =&gt; x.ItemCode == model.ItemCode)
  ...
  ...
  ...
  .ToList();
  </code></pre>