使用StartsWith创建Linq表达式,EndsWith和Contains传递Expression <Func <T,string >>

Rod*_*ero 10 c# linq

我想创建一个传递类型表达式的方法Expression<Func<T, string>来创建类型的表达式Expression<Func<T, bool>>来过滤字符串属性StartsWith,EndsWithContains使用这些表达式之类的方法:

.Where(e => e.MiProperty.ToUpper().StartsWith("ABC"));
.Where(e => e.MiProperty.ToUpper().EndsWith("XYZ"));
.Where(e => e.MiProperty.ToUpper().Contains("MNO"));
Run Code Online (Sandbox Code Playgroud)

该方法应如下所示:

public Expression<Func<T, bool>> AddFilterToStringProperty<T>(Expresssion<Func<T, string>> pMyExpression, string pFilter, FilterType pFiltertype)
Run Code Online (Sandbox Code Playgroud)

其中过滤式是包含三个所提到的操作的枚举类型(StartsWith,EndsWith,Contains)

dtb*_*dtb 9

试试这个:

public static Expression<Func<T, bool>> AddFilterToStringProperty<T>(
    Expression<Func<T, string>> expression, string filter, FilterType type)
{
    return Expression.Lambda<Func<T, bool>>(
        Expression.Call(
            expression.Body,
            type.ToString(),
            null,
            Expression.Constant(filter)),
        expression.Parameters);
}
Run Code Online (Sandbox Code Playgroud)