title: case insensitive dynamic search for linq to entities
body: <p>For the <a href="https://stackoverflow.com/questions/5305526/can-linq-expression-be-case-insensitive">question</a> , the answer suggests that we cannot use the LINQ to SQL or linq to entities.</p>
  
  <p>If I write</p>
  
  <pre><code>Expression toLower = Expression.Call(memberAccess, typeof(T).GetMethod("IndexOf",
                                       new[] { typeof(T), typeof(StringComparison) }));
    Expression  condition = Expression.Call(toLower,
                                  typeof(string).GetMethod("Contains"),
                                  Expression.Constant(value.ToString().ToLower()));
                      lambda = Expression.Lambda(condition, parameter);
  </code></pre>
  
  <p>I get error:</p>
  
  <pre><code>Value cannot be null. Parameter name: method
  </code></pre>
  
  <p>If I write</p>
  
  <pre><code>Expression toLower = Expression.Call(memberAccess, typeof(string).GetMethod("IndexOf",
                                           new[] { typeof(string), typeof(StringComparison) }));
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>Incorrect number of arguments supplied for call to method 'Int32 IndexOf(System.String, System.StringComparison)'
  </code></pre>
  
  <p>Thanks in advance</p>
  
  <p><strong>Edit:</strong>
  Here is the snippet and I need to be able to do a case insenstivie filter:</p>
  
  <pre><code>public static IQueryable&lt;T&gt; Where&lt;T&gt;(this IQueryable&lt;T&gt; query,
              string column, object value, WhereOperation operation)
          {
              if (string.IsNullOrEmpty(column))
                  return query;
  
              ParameterExpression parameter = Expression.Parameter(query.ElementType, "p");
  
              MemberExpression memberAccess = null;
              foreach (var property in column.Split('.'))
                  memberAccess = MemberExpression.Property
                     (memberAccess ?? (parameter as Expression), property);
  
              //change param value type
              //necessary to getting bool from string
              ConstantExpression filter = Expression.Constant
                  (
                      Convert.ChangeType(value, memberAccess.Type)
                  );
  
              //switch operation
              Expression condition = null;
              LambdaExpression lambda = null;
              switch (operation)
              {
                  //equal ==
                  case WhereOperation.Equal:
                      condition = Expression.Equal(memberAccess, filter);
                      lambda = Expression.Lambda(condition, parameter);
                      break;
                  //not equal !=
                  case WhereOperation.NotEqual:
                      condition = Expression.NotEqual(memberAccess, filter);
                      lambda = Expression.Lambda(condition, parameter);
                      break;
                  //string.Contains()
                  case WhereOperation.Contains:
                      condition = Expression.Call(memberAccess,
                          typeof(string).GetMethod("Contains"),
                          Expression.Constant(value));
                      lambda = Expression.Lambda(condition, parameter);
                      break;
              }
  
  
              MethodCallExpression result = Expression.Call(
                     typeof(Queryable), "Where",
                     new[] { query.ElementType },
                     query.Expression,
                     lambda);
  
              return query.Provider.CreateQuery&lt;T&gt;(result);
          }
  </code></pre>