带有ToUpper的字段名称的表达式谓词

时间:2012-07-30 12:18:15

标签: c# linq predicate

我有这段代码:

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;
}

结果是:{Param_0 => Param_0.Username.Contains("MX")}

但我想这样:{Param_0 => Param_0.Username.ToUpper().Contains("MX")}

谢谢,

1 个答案:

答案 0 :(得分: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;
}