C# Expression Tree with Ignore Case or tolower

.net asp.net c# expression-trees

Question

I want to create expression.Equal() with ignore case. Sample code:

if (!string.IsNullOrEmpty(request.BrandCode))
{
    MemberExpression me = Expression.Property(parmExpression, "BrandCode");
    ConstantExpression constant = Expression.Constant(request.BrandCode, typeof(string));
    expbrandCode = Expression.Equal(me, constant);
}
if (!string.IsNullOrEmpty(request.CountryCode))
{
    MemberExpression me = Expression.Property(parmExpression, "CountryCode");
    ConstantExpression constant = Expression.Constant(request.CountryCode, typeof(string));
    expCountryCode = Expression.Equal(me, constant);
}

var finalexpression = Expression.AndAlso(expbrandCode, expCountryCode);

Pass the final expression into a method which accepts the expression.

I tried to create a Methodinfo for Tolower, passed it in Expression.Equal() and that didn't work.

Any guidence would be helpful.

1
0
6/18/2018 10:07:55 PM

Popular Answer

You could use string.Equals(string, string, StringComparison), like:

var expbrandCode = Expression.Call(typeof(string), "Equals", null, me, constant, Expression.Constant(StringComparison.CurrentCultureIgnoreCase));

Note that if you want to use the expression with Entity Framework or similar orms then it becomes complex... Not sure if they support translating that string.Equals() overload.

0
6/19/2018 7:13:05 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow