我有一个复杂的过滤器,部分原因是:
protected override Expression<Func<IHaveNoteRemarkSubject, bool>> MustFilterWhere()
{
if (!Model.ExactSet) return PredicateBuilder.True<IHaveNoteRemarkSubject>();
var mustPartsWhere = PredicateBuilder.True<IHaveNoteRemarkSubject>();
Model.ExactFilterValue.ForEach(str => mustPartsWhere = mustPartsWhere
.And(svi => (svi.Notes ?? string.Empty).Contains(str)));
return mustPartsWhere;
}
我想动态指定字段(当前&#34; Notes&#34;)。也就是说,我将为需要这个确切代码的不同字段设置多个过滤器,我不想单独为每个字段重写此逻辑。反射不起作用,因为这是在服务器端执行的。
答案 0 :(得分:0)
我发现你已经在使用PredicateBuilder
课了。该类有其他辅助方法,您可以使用它们来实现完全动态的过滤器。下面是一个方法的示例,该方法可以引入属性名称(例如“Notes”)并以这种方式构建过滤器:
private Expression<Func<IHaveNoteRemarkSubject, bool>> DynamicMustFilterWhere(IEnumerable<string> exactFilterValues, string propertyName)
{
//Build the individual filters for each filter value
var individualFilters =
exactFilterValues.Select(
//The PredicateBuilder.Make method is very handy in building dynamic filters. In this case, we are dynamically
// specifying the property name to do the Contains on
str => PredicateBuilder.Make(typeof (IHaveNoteRemarkSubject), propertyName, FilterOperator.Contains, str));
//AND all the individual filters together
var combined = PredicateBuilder.And(individualFilters.ToArray<IPredicateDescription>());
//Much of DevForce can use Predicate directly. But if we want to end up with an Expression<Func<...>>, we can do that...
//Have DevForce build the predicate into a lambda expression
var lambda = combined.ToLambdaExpression();
//Then we can cast the lambda to the Expression<Fun<...>> type
return (Expression<Func<IHaveNoteRemarkSubject, bool>>) lambda;
}
您可能会注意到上面的方法没有对contains字段进行任何NULL检查。 DevForce将执行查询的方式,即使字段为NULL也应该有效。 DevForce对这类事情非常聪明。唯一一次(我认为)你需要显式的NULL逻辑,如果你要将这个表达式编译成一个可运行的委托,然后以这种方式调用它。如果你这样做,你可以添加一个额外的调用PredicateBuilder.Make
来检查null'propertyName'值。
有关在DevForce中构建动态过滤器(甚至整个查询)的详细信息,请查看其文档Create dynamic "Where" clauses,Combine predicates with PredicateBuilder和Build queries dynamically