【问题标题】:DevForce Ideablade Filter on dynamic column动态列上的 DevForce Ideablade 过滤器
【发布时间】:2015-01-29 03:07:15
【问题描述】:

我有一个复杂的过滤器,其中一部分是这样的:

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

我想动态指定字段(当前为“Notes”)。也就是说,我将为需要此确切代码的不同字段设置多个过滤器,并且我不想为每个字段单独重写此逻辑。反射不起作用,因为这是在服务器端执行的。

【问题讨论】:

    标签: c# filter devforce


    【解决方案1】:

    我看到您已经在使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      • 2021-03-08
      • 2014-06-09
      相关资源
      最近更新 更多