leora leora - 1 year ago 170
C# Question

can linq expression be case insensitive

i am leveraging this project to use jqgrid to filter and sort collections. The one missing feature is that this example is not doing case insensitive search which i need.

So if a user types in "Test" i want it to match with "TEST", "TeST", etc . .

i have code that looks like this:

case WhereOperation.Equal:
condition = Expression.Equal(memberAccessToString, filter);
lambda = Expression.Lambda(condition, parameter);
case WhereOperation.NotEqual:
condition = Expression.NotEqual(memberAccessToString, filter);
lambda = Expression.Lambda(condition, parameter);
case WhereOperation.Contains:
condition = Expression.Call(memberAccessToString,
lambda = Expression.Lambda(condition, parameter);

is there anyway to have these checks below being case insensitive so "Test" would equal "TEST"


Answer Source

Unfortunately the BCL does not have a Contains overload that allows you to specify case invariance. You will have to grab the correct overload of IndexOf as a workaround (checking to see if the result of IndexOf is greater than zero):

var methodInfo 
    = typeof(string)
            new[] { typeof(string), typeof(StringComparison) });

This MethodInfo accepts a string and a StringComparison which will allow you to specify StringComparison.OrdinalIgnoreCase if you wish.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download