title: Expression tree to create dynamic Where clause throwing error related to params
body: <p>I've got a list of lists of conditions being sent up from the client.  I need to take this list and create a dynamic where clause to be executed by EntityFramework.  </p>
  
  <p>Each condition has an operator, an attribute, and a right-hand-side value.  </p>
  
  <p>Each list of conditions need to be ANDed together.</p>
  
  <p>Each list of lists of conditions needs to be ORed together.</p>
  
  <p>So if we had</p>
  
  <pre><code>{  
     "ConditionLists":[  
        [  
           {  
              "LhsAttributeDefinition":{  
                 "attribute":{  
                    "key":"isHighBandwidth",
                    "value":"IsHighBandwidth"
                 }
              },
              "Operator":{  
                 "name":"equals",
                 "description":"=",
                 "validation":"",
                 "inputType":"dropdown"
              },
              "RhsValue":"true"
           },
           {  
              "LhsAttributeDefinition":{  
                 "attribute":{  
                    "key":"isForMobile",
                    "value":"IsForMobile"
                 }
              },
              "Operator":{  
                 "name":"equals",
                 "description":"=",
                 "validation":"",
                 "inputType":"dropdown"
              },
              "RhsValue":"true"
           }
        ],
        [  
           {  
              "LhsAttributeDefinition":{  
                 "attribute":{  
                    "key":"isHighBandwidth",
                    "value":"IsHighBandwidth"
                 }
              },
              "Operator":{  
                 "name":"equals",
                 "description":"=",
                 "validation":"",
                 "inputType":"dropdown"
              },
              "RhsValue":"true"
           },
           {  
              "LhsAttributeDefinition":{  
                 "attribute":{  
                    "key":"isForTablet",
                    "value":"IsForTablet"
                 }
              },
              "Operator":{  
                 "name":"equals",
                 "description":"=",
                 "validation":"",
                 "inputType":"dropdown"
              },
              "RhsValue":"true"
           }
        ]
     ]
  }
  </code></pre>
  
  <p>That should generate <code>.Where(x =&gt; (x.isHighBandwidth == true &amp;&amp; x.isForMobile == true) || (x.isHighBandwidth == true &amp;&amp; x.isForTablet == true))</code></p>
  
  <p>Here's what I've got to accomplish this using the Expression library:</p>
  
  <pre><code>MethodInfo contains = typeof(string).GetMethod("Contains", new[] { typeof(string) });
  Expression finalExpression = null;
  List&lt;ParameterExpression&gt; paramsArray = new List&lt;ParameterExpression&gt;();
  foreach (var conditionList in conditionLists)
  {
      Expression andGroup = null;
      foreach (var condition in conditionList)
      {
          Expression expression = null;
          ParameterExpression param = null;
          ConstantExpression constant = null;
          switch (condition.LhsAttributeDefinition.Attribute.Key)
          {
              case "title":
                  param = Expression.Parameter(typeof(string), "LearningItem.Title");
                  constant = Expression.Constant(condition.RhsValue, typeof(string));
                  expression = Expression.Call(param, contains, constant);
                  break;
              case "isHighBandwidth":
                  param = Expression.Parameter(typeof(string), "IsHighBandwidth");
                  constant = Expression.Constant(condition.RhsValue, typeof(string));
                  expression = Expression.Equal(param, constant);
  
                  break;
              case "isForMobile":
                  param = Expression.Parameter(typeof(string), "IsForMobile");
                  constant = Expression.Constant(condition.RhsValue, typeof(string));
                  expression = Expression.Equal(param, constant);
  
                  break;
              case "isForTablet":
                  param = Expression.Parameter(typeof(string), "IsForTablet");
                  constant = Expression.Constant(condition.RhsValue, typeof(string));
                  expression = Expression.Equal(param, constant);
  
                  break;
  
          }
          paramsArray.Add(param);
          if (andGroup != null)
          {
              Expression.And(andGroup, expression);
          }
          else
          {
              andGroup = expression;
          }
      }
      //OR the expression tree created above
  
      if (finalExpression != null)
      {
          Expression.Or(finalExpression, andGroup);
      }
      else
      {
          finalExpression = andGroup;
      }
  }
  MethodCallExpression whereCallExpression = Expression.Call(
      typeof(Queryable),
      "Where",
      new Type[] { query.ElementType },
      query.Expression,
      Expression.Lambda&lt;Func&lt;Activity, bool&gt;&gt;(finalExpression, paramsArray.ToArray&lt;ParameterExpression&gt;()));
  return query;
  </code></pre>
  
  <p>So my thought is that inside of a nested for loop, I build the AND queries and the OR queries as one big expression, and then I create the lambda query at the very end.  I collect parameters along the way into a paramsArray (list).</p>
  
  <p>My issue is that when executing, it blows up saying that <code>'ParameterExpression of type 'System.String' cannot be used for delegate parameter of type 'INOLMS.Data.Activity''</code>.  I'm assuming this is because the parameter I've collected so far is just a string (my example request body is just a single condition with IsHighBandwidth true), and it doesn't like that I'm taking a string parameter and trying to get an <code>Activity</code> query.</p>
  
  <p>What am I doing wrong here? </p>