我想创建一个传递类型表达式的方法Expression<Func<T, string>来创建类型的表达式Expression<Func<T, bool>>来过滤字符串属性StartsWith,EndsWith并Contains使用这些表达式之类的方法:
.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)
试试这个:
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)
| 归档时间: |
|
| 查看次数: |
7153 次 |
| 最近记录: |