I need to build custom string expressions based on input and provided operators. However these comparisons are always case sentitive. I need to do the comparison while ignoring case.
static Expression<Func<T, bool>> GetStringExpression<T>(string field, string compareOperator, string value)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameterExp, field);
MethodInfo method = typeof(string).GetMethod(compareOperator, new[] { typeof(string) });
var someValue = Expression.Constant(value, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
usage of current code:
var expression = GetStringExpression<MyClass>("MyProperty", "Contains", "testValue");
var mylist = QueryableList.Where(expression).ToList();
This comparison will not return true when comparing "test" with "TEST", that case should also be true.