0

I have the following class and extension method to invoke String.Contains method. How can I change it to be case insensitive ? Something like in Expression tree for String.IndexOf method but I don't have an ideas so far how to adjust that code into my code. Any help ?

public class testItem
{
    public string SomeProperty { get; set; }
}

public static IQueryable<testItem> PropertyContainsNEW<testItem>(this IQueryable<testItem> source,
                                                                Expression<Func<testItem, string>> selector,
                                                                string value)
{
    ParameterExpression parameter = Expression.Parameter(typeof(testItem), "x");
    Expression property = Expression.Property(parameter, ((MemberExpression)selector.Body).Member.Name);
    var search = Expression.Constant(value, typeof(string));

    MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
    var containsMethodExp = Expression.Call(property, method, search);

    var predicate = Expression.Lambda<Func<testItem, bool>>(containsMethodExp, parameter);

    return source.Where(predicate);
}
Tony
  • 12,405
  • 36
  • 126
  • 226
  • You want it case insensitive in a resulting query execution? If so that is no clear in your question. – Igor Jan 18 '19 at 13:04
  • yes, I provide the collection, pick a property and provide a string to be compared – Tony Jan 18 '19 at 13:06
  • https://referencesource.microsoft.com/#mscorlib/system/string.cs,2172 why do you need Contains rather than just using IndexOf to achieve your purpose? – Alexandru Clonțea Jan 18 '19 at 13:08
  • Case sensitivity is usually a database instance setting. Sql Server, for example, uses case insensitive collation by default during install. – Igor Jan 18 '19 at 13:13
  • @AlexandruClonțea that's the point, I need to use IndexOf with the StringComparison.OrdinalIgnoreCase option here – Tony Jan 18 '19 at 13:16
  • But, isn't there an accepted answer to https://stackoverflow.com/questions/7089225/expression-tree-for-string-indexof-method. It seems correct to me. – Alexandru Clonțea Jan 18 '19 at 13:17
  • @AlexandruClonțea there is, but I have issues with adapting that code into my code – Tony Jan 18 '19 at 13:21

1 Answers1

0

In order to use the StringComparison parameter, you need to correctly identify that method.

Is this what you need?:

public static IQueryable<testItem> PropertyContainsNEW(this IQueryable<testItem> source,
        Expression<Func<testItem, string>> selector,
        string value)
{
   var parameter = Expression.Parameter(typeof(testItem), "x");
   var property = Expression.Property(parameter, ((MemberExpression)selector.Body).Member.Name);
   var search = Expression.Constant(value, typeof(string));

   var parms = new Expression[] { search,
            Expression.Constant(StringComparison.OrdinalIgnoreCase) };

   var method = typeof(string).GetMethod("Contains", new[] { typeof(string), typeof(StringComparison) });
   var containsMethodExp = Expression.Call(property, method, parms);

   var predicate = Expression.Lambda<Func<testItem, bool>>(containsMethodExp, parameter);

   return source.Where(predicate);
}
Alexandru Clonțea
  • 1,746
  • 13
  • 23