title: How to Create a Linq Query with Multiple Conditional Where Clauses
body: <p>I have a linq query with multiple conditional where clauses. The query returns all data in the table when there is no filter in the where clause. How to make the linq query returns 0 record in the first time when there is no filter in the where clause? See my code below:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  using System.Web.Http;
  using API.Models;
  using System.Linq.Expressions;
  namespace API.Controllers
  {
  </code></pre>
  
  <p>//api code starts here</p>
  
  <pre><code>public class MYTABLEController : ApiController
  {
      private DataModel db = new DataModel();
  
  
  
      //GET /MYTABLE
      public List&lt;MYTABLE&gt; Get(string filter1 = null,string filter2=null)
      {
  
          IQueryable&lt;MYTABLE&gt; qry = db.MYTABLE.AsQueryable();
          var searchPredicate = PredicateBuilder.False&lt;MYTABLE&gt;();
  
          if (!string.IsNullOrWhiteSpace(filter1))
          {
  
              searchPredicate = searchPredicate.And(a =&gt; a.COLUMN1==(filter1);
  
          }
          if (!string.IsNullOrWhiteSpace(filter2))
          {
  
              searchPredicate = searchPredicate.And(a =&gt; a.COLUMN2==(filter2);
  
          }
  
          return qry.Where(searchPredicate).ToList();
  
      }
  </code></pre>
  
  <p>//PredicateBuilder <a href="https://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/" rel="nofollow noreferrer">https://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/</a></p>
  
  <pre><code>      public static class PredicateBuilder
  {
      /// &lt;summary&gt;
      /// Creates a predicate that evaluates to true.
      /// &lt;/summary&gt;
      public static Expression&lt;Func&lt;T, bool&gt;&gt; True&lt;T&gt;() { return param =&gt; true; }
  
      /// &lt;summary&gt;
      /// Creates a predicate that evaluates to false.
      /// &lt;/summary&gt;
      public static Expression&lt;Func&lt;T, bool&gt;&gt; False&lt;T&gt;() { return param =&gt; false; }
  
      /// &lt;summary&gt;
      /// Creates a predicate expression from the specified lambda expression.
      /// &lt;/summary&gt;
      public static Expression&lt;Func&lt;T, bool&gt;&gt; Create&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; predicate) { return predicate; }
  
      /// &lt;summary&gt;
      /// Combines the first predicate with the second using the logical "and".
      /// &lt;/summary&gt;
      public static Expression&lt;Func&lt;T, bool&gt;&gt; And&lt;T&gt;(this Expression&lt;Func&lt;T, bool&gt;&gt; first, Expression&lt;Func&lt;T, bool&gt;&gt; second)
      {
          return first.Compose(second, Expression.AndAlso);
      }
  
      /// &lt;summary&gt;
      /// Combines the first predicate with the second using the logical "or".
      /// &lt;/summary&gt;
      public static Expression&lt;Func&lt;T, bool&gt;&gt; Or&lt;T&gt;(this Expression&lt;Func&lt;T, bool&gt;&gt; first, Expression&lt;Func&lt;T, bool&gt;&gt; second)
      {
          return first.Compose(second, Expression.OrElse);
      }
  
      /// &lt;summary&gt;
      /// Negates the predicate.
      /// &lt;/summary&gt;
      public static Expression&lt;Func&lt;T, bool&gt;&gt; Not&lt;T&gt;(this Expression&lt;Func&lt;T, bool&gt;&gt; expression)
      {
          var negated = Expression.Not(expression.Body);
          return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(negated, expression.Parameters);
      }
  
      /// &lt;summary&gt;
      /// Combines the first expression with the second using the specified merge function.
      /// &lt;/summary&gt;
      static Expression&lt;T&gt; Compose&lt;T&gt;(this Expression&lt;T&gt; first, Expression&lt;T&gt; second, Func&lt;Expression, Expression, Expression&gt; merge)
      {
          // zip parameters (map from parameters of second to parameters of first)
          var map = first.Parameters
              .Select((f, i) =&gt; new { f, s = second.Parameters[i] })
              .ToDictionary(p =&gt; p.s, p =&gt; p.f);
  
          // replace parameters in the second lambda expression with the parameters in the first
          var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
  
          // create a merged lambda expression with parameters from the first expression
          return Expression.Lambda&lt;T&gt;(merge(first.Body, secondBody), first.Parameters);
      }
  
      class ParameterRebinder : ExpressionVisitor
      {
          readonly Dictionary&lt;ParameterExpression, ParameterExpression&gt; map;
  
          ParameterRebinder(Dictionary&lt;ParameterExpression, ParameterExpression&gt; map)
          {
              this.map = map ?? new Dictionary&lt;ParameterExpression, ParameterExpression&gt;();
          }
  
          public static Expression ReplaceParameters(Dictionary&lt;ParameterExpression, ParameterExpression&gt; map, Expression exp)
          {
              return new ParameterRebinder(map).Visit(exp);
          }
  
          protected override Expression VisitParameter(ParameterExpression p)
          {
              ParameterExpression replacement;
  
              if (map.TryGetValue(p, out replacement))
              {
                  p = replacement;
              }
  
              return base.VisitParameter(p);
          }
      }
  }
  </code></pre>