title: Combining AndAlso The parameter 'foo' was not bound in the specified LINQ to Entities query expression
body: <p>I have an entity.</p>
  
  <pre><code>public class Foo
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string Code { get; set; }
  }
  </code></pre>
  
  <p>I want to create my own expression predicate. For that I have created a method that accepts property name and the value.</p>
  
  <pre><code>private static Expression&lt;Func&lt;Foo, bool&gt;&gt; Condition(string pName, object value)
  {
      var pe = Expression.Parameter(typeof(Foo), "foo");
      var left = Expression.Property(pe, pName);
      var right = Expression.Constant(value);
      var equal = Expression.Equal(left, right);
      var predicate = Expression.Lambda&lt;Func&lt;Foo, bool&gt;&gt;(equal, pe);
      return predicate;
  }
  </code></pre>
  
  <p>This is the predicate which works fine for a single condition.</p>
  
  <pre><code>using (var db = new MyEntities())
  {
      var predicate = Condition("Name", "foo");
      var foos = db.Foos.Where(predicate).ToArray();
  }
  </code></pre>
  
  <p>But when I tried to combine two conditions by following <a href="https://stackoverflow.com/questions/457316/combining-two-expressions-expressionfunct-bool">this post</a>, it throws exception.</p>
  
  <blockquote>
    <p>The parameter 'foo' was not bound in the specified LINQ to Entities
    query expression.</p>
  </blockquote>
  
  <pre><code>using (var db = new MyEntities())
  {
      var cond1 = Condition("Name", "foo");
      var cond2 = Condition("Code", "bar");
      var body = Expression.AndAlso(cond1.Body, cond2.Body);
      var predicate = Expression.Lambda&lt;Func&lt;Foo,bool&gt;&gt;(body, cond1.Parameters[0]);
      var foos = db.Foos.Where(predicate).ToArray(); // exception
  }
  </code></pre>
  
  <p>Please enlighten me.</p>