1

我有这个代码:

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")}

谢谢,

4

1 回答 1

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;
}
于 2012-07-30T12:27:09.890 回答