title: How do I combine LINQ expressions into one?
body: <p>I've got a form with multiple fields on it (company name, postcode, etc.) which allows a user to search for companies in a database. If the user enters values in more than one field then I need to search on all of those fields. I am using LINQ to query the database.</p>
  
  <p>So far I have managed to write a function which will look at their input and turn it into a List of expressions. I now want to turn that List into a single expression which I can then execute via the LINQ provider.</p>
  
  <p>My initial attempt was as follows</p>
  
  <pre><code>private Expression&lt;Func&lt;Company, bool&gt;&gt; Combine(IList&lt;Expression&lt;Func&lt;Company, bool&gt;&gt;&gt; expressions)
      {
          if (expressions.Count == 0)
          {
              return null;
          }
          if (expressions.Count == 1)
          {
              return expressions[0];
          }
          Expression&lt;Func&lt;Company, bool&gt;&gt; combined = expressions[0];
          expressions.Skip(1).ToList().ForEach(expr =&gt; combined = Expression.And(combined, expr));
          return combined;
      }
  </code></pre>
  
  <p>However this fails with an exception message along the lines of "The binary operator And is not defined for...". Does anyone have any ideas what I need to do to combine these expressions?</p>
  
  <p>EDIT: Corrected the line where I had forgotten to assign the result of and'ing the expressions together to a variable. Thanks for pointing that out folks.</p>