title: The dreaded "parameter was not bound in the specified LINQ to Entities query expression" exception
body: <p>I am trying to get an understanding of expressions. I am hitting the dreaded "parameter was not bound in the specified LINQ to Entities query expression" exception. I have seen Skeet answer this before as well as others online. However I just cannot "see" the problem. I am using Colin Meek's example <a href="http://blogs.msdn.com/b/meek/archive/2008/05/02/linq-to-entities-combining-predicates.aspx">http://blogs.msdn.com/b/meek/archive/2008/05/02/linq-to-entities-combining-predicates.aspx</a> and Matt Warren's VisitorExpression class <a href="http://blogs.msdn.com/b/mattwar/archive/2007/07/31/linq-building-an-iqueryable-provider-part-ii.aspx">http://blogs.msdn.com/b/mattwar/archive/2007/07/31/linq-building-an-iqueryable-provider-part-ii.aspx</a>. I'm using a console app in VS 2010, framework 4.0. Sorry if this is beating a dead horse since it has been asked about before. Thanks for any help.</p>
  
  <p>To be thorough, all the code is below:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Expression&lt;Func&lt;Car, bool&gt;&gt; theCarIsRed = c =&gt; c.Color == "Red";
          Expression&lt;Func&lt;Car, bool&gt;&gt; theCarIsCheap = c =&gt; c.Price &lt; 10.0;
          Expression&lt;Func&lt;Car, bool&gt;&gt; theCarIsRedOrCheap = theCarIsRed.Or(theCarIsCheap);
  
          Car one = new Car() { CarId = 1, Color = "Red", Price = 10.0 };
          Car two = new Car() { CarId = 2, Color = "Blue", Price = 9.0 };
          Car three = new Car() { CarId = 3, Color = "Black", Price = 5.0 };
          Car four = new Car() { CarId = 4, Color = "Red", Price = 5.0 };
  
          EntityDBContext context = new EntityDBContext();
          context.CarEntity.Add(one);
          context.CarEntity.Add(two);
          context.CarEntity.Add(three);
          context.CarEntity.Add(four);
  
          using (var db = context)
          {
              // Bang
              var query = db.CarEntity.AsQueryable().Where(theCarIsRedOrCheap).ToList();
  
              foreach (var item in query)
              {
                  Console.WriteLine(item.Color, item.Price);
              }
          }
  
          Console.ReadKey();
      }
  }
  
  public class Car 
  {
      [Key]
      public int CarId { get; set; }
      public string Color { get; set; }
      public double Price { get; set; }
  }
  
  
  public static class Utility
  {
      public 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)
      {
          var map = first.Parameters.Select((f, i) =&gt; new { f, s = second.Parameters[i] }).ToDictionary(p =&gt; p.s, p =&gt; p.f);
  
          var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
  
          return Expression.Lambda&lt;T&gt;(merge(first.Body, second.Body), first.Parameters);
      }
  
      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.And);
      }
  
      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.Or);
      }
  }
  
  
  public class ParameterRebinder : ExpressionVisitor
  {
      private readonly Dictionary&lt;ParameterExpression, ParameterExpression&gt; map;
  
      public 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 parmEx)
      {
          ParameterExpression replacement;
          if (map.TryGetValue(parmEx, out replacement))
          {
              parmEx = replacement;
          }
  
          return base.VisitParameter(parmEx);
      }
  }
  
  public abstract class ExpressionVisitor
  {
      protected ExpressionVisitor()
      {
      }
  
      protected virtual Expression Visit(Expression exp)
      {
          if (exp == null)
          {
              return exp;
          }
  
          switch (exp.NodeType)
          {
              case ExpressionType.Negate:
              case ExpressionType.NegateChecked:
              case ExpressionType.Not:
              case ExpressionType.Convert:
              case ExpressionType.ConvertChecked:
              case ExpressionType.ArrayLength:
              case ExpressionType.Quote:
              case ExpressionType.TypeAs:
                  return this.VisitUnary((UnaryExpression) exp);
              case ExpressionType.Add:
              case ExpressionType.AddChecked:
              case ExpressionType.Subtract:
              case ExpressionType.SubtractChecked:
              case ExpressionType.Multiply:
              case ExpressionType.MultiplyChecked:
              case ExpressionType.Divide:
              case ExpressionType.Modulo:
              case ExpressionType.And:
              case ExpressionType.AndAlso:
              case ExpressionType.Or:
              case ExpressionType.OrElse:
              case ExpressionType.LessThan:
              case ExpressionType.LessThanOrEqual:
              case ExpressionType.GreaterThan:
              case ExpressionType.GreaterThanOrEqual:
              case ExpressionType.Equal:
              case ExpressionType.NotEqual:
              case ExpressionType.Coalesce:
              case ExpressionType.ArrayIndex:
              case ExpressionType.RightShift:
              case ExpressionType.LeftShift:
              case ExpressionType.ExclusiveOr:
                  return this.VisitBinary((BinaryExpression) exp);
              case ExpressionType.TypeIs:
                  return this.VisitTypeIs((TypeBinaryExpression) exp);
              case ExpressionType.Conditional:
                  return this.VisitConditional((ConditionalExpression) exp);
              case ExpressionType.Constant:
                  return this.VisitConstant((ConstantExpression) exp);
              case ExpressionType.Parameter:
                  return this.VisitParameter((ParameterExpression) exp);
              case ExpressionType.MemberAccess:
                  return this.VisitMemberAccess((MemberExpression) exp);
              case ExpressionType.Call:
                  return this.VisitMethodCall((MethodCallExpression) exp);
              case ExpressionType.Lambda:
                  return this.VisitLambda((LambdaExpression) exp);
              case ExpressionType.New:
                  return this.VisitNew((NewExpression) exp);
              case ExpressionType.NewArrayInit:
              case ExpressionType.NewArrayBounds:
                  return this.VisitNewArray((NewArrayExpression) exp);
              case ExpressionType.Invoke:
                  return this.VisitInvocation((InvocationExpression) exp);
              case ExpressionType.MemberInit:
                  return this.VisitMemberInit((MemberInitExpression) exp);
              case ExpressionType.ListInit:
                  return this.VisitListInit((ListInitExpression) exp);
              default:
                  throw new Exception(String.Format("Unhandled expression type: '{0}'", exp.NodeType));
  
          }
      }
  
      protected virtual MemberBinding VisitBinding(MemberBinding binding)
      {
          switch (binding.BindingType)
          {
              case MemberBindingType.Assignment:
                  return this.VisitMemberAssignment((MemberAssignment) binding);
                  case MemberBindingType.MemberBinding:
                  return this.VisitMemberMemberBinding((MemberMemberBinding) binding);
                  case MemberBindingType.ListBinding:
                  return this.VisitMemberListBinding((MemberListBinding) binding);
              default:
                  throw new Exception(String.Format("Unhandled binding type '{0}'", binding.BindingType));
          }
      }
  
      protected virtual Expression VisitListInit(ListInitExpression init)
      {
          NewExpression n = this.VisitNew(init.NewExpression);
          IEnumerable&lt;ElementInit&gt; initializers = this.VisitElementInitializerList(init.Initializers);
  
          if (n != init.NewExpression || initializers != init.Initializers)
          {
              return Expression.ListInit(n, initializers);
          }
  
          return init;
      }
  
      protected virtual Expression VisitMemberInit(MemberInitExpression memberInitExpression)
      {
          NewExpression n = this.VisitNew(memberInitExpression.NewExpression);
          IEnumerable&lt;MemberBinding&gt; bindings = this.VisitBindingList(memberInitExpression.Bindings);
  
          if (n != memberInitExpression.NewExpression || bindings != memberInitExpression.Bindings)
          {
              return Expression.MemberInit(n, bindings);
          }
  
          return memberInitExpression;
      }
  
      protected virtual Expression VisitInvocation(InvocationExpression invocationExpression)
      {
          IEnumerable&lt;Expression&gt; args = this.VisitExpressionList(invocationExpression.Arguments);
          Expression expr = this.Visit(invocationExpression.Expression);
  
          if (args != invocationExpression.Arguments || expr != invocationExpression.Expression)
          {
              return Expression.Invoke(expr, args);
          }
  
          return invocationExpression;
      }
  
      protected virtual Expression VisitNewArray(NewArrayExpression newArrayExpression)
      {
          IEnumerable&lt;Expression&gt; exprs = this.VisitExpressionList(newArrayExpression.Expressions);
  
          if (exprs != newArrayExpression.Expressions)
          {
              if (newArrayExpression.NodeType == ExpressionType.NewArrayInit)
              {
                  return Expression.NewArrayInit(newArrayExpression.Type.GetElementType(), exprs);
              }
              else
              {
                  return Expression.NewArrayBounds(newArrayExpression.Type.GetElementType());
              }
          }
  
          return newArrayExpression;
      }
  
      protected virtual NewExpression VisitNew(NewExpression newExpression)
      {
          IEnumerable&lt;Expression&gt; args = this.VisitExpressionList(newExpression.Arguments);
  
          if (args != newExpression.Arguments)
          {
              if (newExpression.Members != null)
              {
                  return Expression.New(newExpression.Constructor, args, newExpression.Members);
              }
              else
              {
                  return Expression.New(newExpression.Constructor, args);
              }
          }
  
          return newExpression;
      }
  
      protected virtual Expression VisitLambda(LambdaExpression lambdaExpression)
      {
          Expression body = this.Visit(lambdaExpression.Body);
          if (body != lambdaExpression.Body)
          {
              return Expression.Lambda(lambdaExpression.Type, body, lambdaExpression.Parameters);
          }
  
          return lambdaExpression;
      }
  
      protected virtual Expression VisitMethodCall(MethodCallExpression methodCallExpression)
      {
          Expression obj = this.Visit(methodCallExpression.Object);
          IEnumerable&lt;Expression&gt; args = this.VisitExpressionList(methodCallExpression.Arguments);
  
          if (obj != methodCallExpression.Object || args != methodCallExpression.Arguments)
          {
              return Expression.Call(obj, methodCallExpression.Method, args);
          }
  
          return methodCallExpression;
      }
  
      protected virtual Expression VisitMemberAccess(MemberExpression memberExpression)
      {
          Expression exp = this.Visit(memberExpression.Expression);
  
          if (exp != memberExpression.Expression)
          {
              return Expression.MakeMemberAccess(exp, memberExpression.Member);
          }
  
          return memberExpression;
      }
  
      protected virtual Expression VisitParameter(ParameterExpression parameterExpression)
      {
          return parameterExpression;
      }
  
      protected virtual Expression VisitConstant(ConstantExpression constantExpression)
      {
          return constantExpression;
      }
  
      protected virtual Expression VisitConditional(ConditionalExpression conditionalExpression)
      {
          Expression test = this.Visit(conditionalExpression.Test);
          Expression ifTrue = this.Visit(conditionalExpression.IfTrue);
          Expression ifFalse = this.Visit(conditionalExpression.IfFalse);
  
          if (test != conditionalExpression.Test || ifTrue != conditionalExpression.IfTrue || ifFalse != conditionalExpression.IfFalse)
          {
              return Expression.Condition(test, ifTrue, ifFalse);
          }
  
          return conditionalExpression;
      }
  
      protected virtual Expression VisitTypeIs(TypeBinaryExpression typeBinaryExpression)
      {
          Expression expr = this.Visit(typeBinaryExpression.Expression);
          if (expr != typeBinaryExpression)
          {
              return Expression.TypeIs(expr, typeBinaryExpression.TypeOperand);
          }
  
          return typeBinaryExpression;
      }
  
      protected virtual Expression VisitBinary(BinaryExpression binaryExpression)
      {
          Expression left = this.Visit(binaryExpression.Left);
          Expression right = this.Visit(binaryExpression.Right);
          Expression conversion = this.Visit(binaryExpression.Conversion);
  
          if (left != binaryExpression.Left || right != binaryExpression.Right || conversion != binaryExpression.Conversion)
          {
              if (binaryExpression.NodeType == ExpressionType.Coalesce &amp;&amp; binaryExpression.Conversion != null)
              {
                  return Expression.Coalesce(left, right, conversion as LambdaExpression);
              }
              else
              {
                  return Expression.MakeBinary(binaryExpression.NodeType, left, right, binaryExpression.IsLiftedToNull,
                                               binaryExpression.Method);
              }
          }
  
          return binaryExpression;
      }
  
      protected virtual Expression VisitUnary(UnaryExpression unaryExpression)
      {
          Expression operand = this.Visit(unaryExpression.Operand);
          if (operand != unaryExpression.Operand)
          {
              return Expression.MakeUnary(unaryExpression.NodeType, operand, unaryExpression.Type,
                                          unaryExpression.Method);
          }
  
          return unaryExpression;
      }
  
      protected virtual ReadOnlyCollection&lt;Expression&gt; VisitExpressionList(ReadOnlyCollection&lt;Expression&gt; original)
      {
          List&lt;Expression&gt; list = null;
  
          for (int i = 0, n = original.Count; i &lt; n; i++)
          {
              Expression p = this.Visit(original[i]);
              if (list != null)
              {
                  list.Add(p);
              }
              else if (p != original[i])
              {
                  list = new List&lt;Expression&gt;(n);
                  for (int j = 0; j &lt; i; j++)
                  {
                      list.Add(original[j]);
                  }
  
                  list.Add(p);
              }
          }
  
          return list != null ? list.AsReadOnly() : original;
      }
  
      protected virtual IEnumerable&lt;MemberBinding&gt; VisitBindingList(ReadOnlyCollection&lt;MemberBinding&gt; original)
      {
          List&lt;MemberBinding&gt; list = null;
  
          for (int i = 0, n = original.Count; i &lt; n; i++)
          {
              MemberBinding b = this.VisitBinding(original[i]);
  
              if (list != null)
              {
                  list.Add(b);
              }
              else if (b != original[i])
              {
                  list = new List&lt;MemberBinding&gt;(n);
  
                  for (int j = 0; j &lt; i; j++)
                  {
                      list.Add(original[j]);
                  }
  
                  list.Add(b);
              }
          }
  
          if (list != null)
          {
              return list;
          }
  
          return original;
      }
  
      protected virtual MemberAssignment VisitMemberAssignment(MemberAssignment assignment)
      {
          Expression e = this.Visit(assignment.Expression);
  
          if (e != assignment.Expression)
          {
              return Expression.Bind(assignment.Member, e);
          }
  
          return assignment;
      }
  
      protected virtual MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding binding)
      {
          IEnumerable&lt;MemberBinding&gt; bindings = this.VisitBindingList(binding.Bindings);
  
          if (bindings != binding.Bindings)
          {
              return Expression.MemberBind(binding.Member, bindings);
          }
  
          return binding;
      }
  
      protected virtual MemberListBinding VisitMemberListBinding(MemberListBinding binding)
      {
          IEnumerable&lt;ElementInit&gt; initializers = this.VisitElementInitializerList(binding.Initializers);
  
          if (initializers != binding.Initializers)
          {
              return Expression.ListBind(binding.Member, initializers);
          }
  
          return binding;
      }
  
      protected virtual IEnumerable&lt;ElementInit&gt; VisitElementInitializerList(ReadOnlyCollection&lt;ElementInit&gt; original)
      {
          List&lt;ElementInit&gt; list = null;
  
          for (int i = 0, n = original.Count; i &lt; n; i++)
          {
              ElementInit init = this.VisitElementInitializer(original[i]);
  
              if (list != null)
              {
                  list.Add(init);
              }
              else if (init != original[i])
              {
                  list = new List&lt;ElementInit&gt;(n);
  
                  for (int j = 0; j &lt; i; j++)
                  {
                      list.Add(original[j]);
                  }
  
                  list.Add(init);
              }
          }
  
          if (list != null)
          {
              return list;
          }
  
          return original;
      }
  
      protected virtual ElementInit VisitElementInitializer(ElementInit initializer)
      {
          ReadOnlyCollection&lt;Expression&gt; arguments = this.VisitExpressionList(initializer.Arguments);
  
          if (arguments != initializer.Arguments)
          {
              return Expression.ElementInit(initializer.AddMethod, arguments);
          }
  
          return initializer;
      }
  }
  </code></pre>