I struggle with adding ToLower() to the Contains LINQ Expressions:
Here is the code of mine with dynamic LINQ Expressions (Contains):
private static Expression GetExpressionCase<T>(ParameterExpression param, SearchCriteria searchCriteria)
{
MethodInfo containsMethod =
typeof(string).GetMethod("Contains", new[] { typeof(string) });
MemberExpression member =
Expression.Property(param, searchCriteria.Key);
ConstantExpression constant =
Expression.Constant(Convert.ChangeType(searchCriteria.Value, member.Type));
switch (searchCriteria.Operation)
{
case '=':
return Expression.Equal(member, constant);
case '>':
return Expression.GreaterThanOrEqual(member, constant);
case '<':
return Expression.LessThanOrEqual(member, constant);
case ':':
return Expression.Call(member, containsMethod, constant);
}
return null;
}
My codes works fine but i want to add ToLower() before Contains() like this query:
Current query looks likes this (Contains() only):
var test = context.Table.Where(x => x.Key.Contains("value"));
I hope query will look likes this (Added ToLower()):
var test = context.Table.Where(x => x.Key.ToLower().Contains("hoa"));
I’ve already tried using StringComparison.OrdinalIgnoreCase but it can’t be translated by the query provider (postgreSQL).
3
Answers
You’re doing it well, I only would suggest simply chaining the result of the
ToLower
call withContains
.Try the following realisation. I have simplified it a little bit.