1

I have this code :

public static Expression<Func<T, bool>> CreatePredicate<T>(string typeSearch, string searchField, string stringToSearch)
{
    var parameter = Expression.Parameter(typeof(T));
    var predicate = Expression.Lambda<Func<T, bool>>(
        Expression.Call(
            Expression.PropertyOrField(parameter, searchField),
            "Contains", null,
            Expression.Constant(stringToSearch.ToUpper())), parameter);

    return predicate;
}

the result is : {Param_0 => Param_0.Username.Contains("MX")}

But I'd like this : {Param_0 => Param_0.Username.ToUpper().Contains("MX")}

Thanks,

TheBoubou
  • 19,487
  • 54
  • 148
  • 236

1 Answers1

2
public static Expression<Func<T, bool>> CreatePredicate<T>(string typeSearch, string searchField, string stringToSearch)
{
    var parameter = Expression.Parameter(typeof(T));
    var predicate = Expression.Lambda<Func<T, bool>>(
        Expression.Call(
            Expression.Call(Expression.PropertyOrField(parameter, searchField), "ToUpper", null),
            "Contains", null,
            Expression.Constant(stringToSearch.ToUpper())), parameter);

    return predicate;
}
Sergei Rogovtcev
  • 5,804
  • 2
  • 22
  • 35
  • Bonus. I have some string with letters with an accent "é,è, ...". Is there a way to treat 'é' as 'e' ? – TheBoubou Jul 30 '12 at 12:45
  • You'll have to look into `CultureInfo culture` parameter of `ToUpper`. – Sergei Rogovtcev Jul 30 '12 at 12:46
  • I don't understand your problem. Begin by writing normal, not generated code, which does what you want, *then* convert it to expression. – Sergei Rogovtcev Jul 30 '12 at 12:59
  • when I do a search on "élève" (student in English) and I do a search contains with "eve", I have no result but I have a result with "ève". I'd like if I search with "ève" or "eve" get "élève" as result. I don't want to consider the accent in the search – TheBoubou Jul 30 '12 at 13:06
  • http://stackoverflow.com/questions/359827/ignoring-accented-letters-in-string-comparison – Sergei Rogovtcev Jul 30 '12 at 13:11
  • I saw that but I don't want remove or change my list, I'd like, if possible, add "Culture Info" to the ToUpper in the ExpressionCall and avoid the loop – TheBoubou Jul 30 '12 at 13:15
  • Either way that has nothing with this question. Please close this one and open another. – Sergei Rogovtcev Jul 30 '12 at 13:35