3

所以,这相当复杂。

我在一个集合中有一组规则,一个规则包含这三个属性。

Field, Op, and Data (all strings)

所以规则可能看起来像“State”、“eq”、“CA”

我的一般规则是所有规则都被 AND 在一起。但是,需要注意的是,如果它们具有相同的 Field 值,则它们都是 ORed。这允许我们说“State”、“eq”、“CA”、或“State”、“eq”、“TX”和“FirstName”、“eq”、“John”。

问题是我当前应用规则的方式行不通,因为它只是使用每个规则不断构建 linq 表达式以使其越来越明确。

var result = rules.Aggregate(_repository.All, (current, rule) => current.ExtendQuery(rule))

ExtendQuery是我写的一个扩展方法,它使用 ExpressionTrees,生成一个新的查询,将当前规则应用于传入的查询。(有效地将它们全部放在一起)

现在我不难修改.Aggregate行以按字段对规则进行分组,然后为每个字段生成一个唯一的查询,但是我如何将它们“或”在一起而不是“与”?

然后对于这些查询中的每一个,我将如何将它们“与”在一起?联盟?

ExtendQuery 看起来像这样

    public static IQueryable<T> ExtendQuery<T>(this IQueryable<T> query, QueryableRequestMessage.WhereClause.Rule rule) where T : class
    {
        var parameter = Expression.Parameter(typeof(T), "x");
        Expression property = Expression.Property(parameter, rule.Field);
        var type = property.Type;

        ConstantExpression constant;
        if (type.IsEnum)
        {
            var enumeration = Enum.Parse(type, rule.Data);
            var intValue = (int)enumeration;
            constant = Expression.Constant(intValue);
            type = typeof(int);
            //Add "Id" by convention, this is all because enum support is lacking at this point in Entity Framework
            property = Expression.Property(parameter, rule.Field + "Id");
        }
        else if(type == typeof(DateTime))
        {
            constant = Expression.Constant(DateTime.ParseExact(rule.Data, "dd/MM/yyyy", CultureInfo.CurrentCulture));
        }
        else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
        {
            //This will convert rule.Data to the baseType, not a nullable type (because that won't work)
            var converter = TypeDescriptor.GetConverter(type);
            var value = converter.ConvertFrom(rule.Data);
            constant = Expression.Constant(value);

            //We change the type of property to get converted to it's base type
            //This is because Expression.GreaterThanOrEqual can't compare a decimal with a Nullable<decimal>
            var baseType = type.GetTypeOfNullable();
            property = Expression.Convert(property, baseType);
        }
        else
        {
            constant = Expression.Constant(Convert.ChangeType(rule.Data, type));
        }

        switch (rule.Op)
        {
            case "eq": //Equals
            case "ne": //NotEquals
                {
                    var condition = rule.Op.Equals("eq")
                                        ? Expression.Equal(property, constant)
                                        : Expression.NotEqual(property, constant);
                    var lambda = Expression.Lambda(condition, parameter);
                    var call = Expression.Call(typeof(Queryable), "Where", new[] { query.ElementType }, query.Expression, lambda);
                    query = query.Provider.CreateQuery<T>(call);
                    break;
                }
            case "lt": //Less Than
                    query = type == typeof (String) 
                        ? QueryExpressionString(query, Expression.LessThan, type, property, constant, parameter)
                        : QueryExpression(query, Expression.LessThan, property, constant, parameter); break;
            case "le": //Less Than or Equal To
                query = type == typeof (String)
                        ? QueryExpressionString(query, Expression.LessThanOrEqual, type, property, constant, parameter)
                        : QueryExpression(query, Expression.LessThanOrEqual, property, constant, parameter); break;
            case "gt": //Greater Than
                query = type == typeof (String)
                        ? QueryExpressionString(query, Expression.GreaterThan, type, property, constant, parameter)
                        : QueryExpression(query, Expression.GreaterThan, property, constant, parameter); break;
            case "ge": //Greater Than or Equal To 
                query = type == typeof (String)
                        ? QueryExpressionString(query, Expression.GreaterThanOrEqual, type, property, constant, parameter)
                        : QueryExpression(query, Expression.GreaterThanOrEqual, property, constant, parameter); break;
            case "bw": //Begins With
            case "bn": //Does Not Begin With
                query = QueryMethod(query, rule, type, "StartsWith", property, constant, "bw", parameter); break;
            case "ew": //Ends With
            case "en": //Does Not End With
                query = QueryMethod(query, rule, type, "EndsWith", property, constant, "cn", parameter); break;
            case "cn": //Contains
            case "nc": //Does Not Contain
                query = QueryMethod(query, rule, type, "Contains", property, constant, "cn", parameter); break;
            case "nu": //TODO: Null
            case "nn": //TODO: Not Null
                break;
        }

        return query;
    }

    private static IQueryable<T> QueryExpression<T>(
        IQueryable<T> query,
        Func<Expression, Expression, BinaryExpression> expression,
        Expression property,
        Expression value,
        ParameterExpression parameter
    ) where T : class
    {
        var condition = expression(property, value);
        var lambda = Expression.Lambda(condition, parameter);
        var call = Expression.Call(typeof(Queryable), "Where", new[] { query.ElementType }, query.Expression, lambda);
        query = query.Provider.CreateQuery<T>(call);
        return query;
    }

    private static IQueryable<T> QueryExpressionString<T>(
        IQueryable<T> query,
        Func<Expression, Expression, BinaryExpression> expression,
        Type type, 
        Expression property, 
        Expression value, 
        ParameterExpression parameter)
    {
        var containsmethod = type.GetMethod("CompareTo", new[] { type });
        var callContains = Expression.Call(property, containsmethod, value);
        var call = expression(callContains, Expression.Constant(0, typeof(int)));
        return query.Where(Expression.Lambda<Func<T, bool>>(call, parameter));
    }

    private static IQueryable<T> QueryMethod<T>(
        IQueryable<T> query,
        QueryableRequestMessage.WhereClause.Rule rule,
        Type type,
        string methodName,
        Expression property,
        Expression value,
        string op,
        ParameterExpression parameter
    ) where T : class
    {
        var containsmethod = type.GetMethod(methodName, new[] { type });
        var call = Expression.Call(property, containsmethod, value);
        var expression = rule.Op.Equals(op)
                             ? Expression.Lambda<Func<T, bool>>(call, parameter)
                             : Expression.Lambda<Func<T, bool>>(Expression.IsFalse(call), parameter);
        query = query.Where(expression);
        return query;
    }
4

3 回答 3

1

您可以使用PredicateBuilderLINQKit来执行此操作。如果Expression为每个规则创建一个,则可以使用And()andOr()方法来组合它们(可能使用PredicateBuilder.True()andFalse()作为基本案例)。最后,调用Expand(),以便查询采用您的查询提供者可以理解的形式。

假设您更改ExtendQuery()为 return anExpression并将其重命名为CreateRuleQuery(),您的代码可能如下所示:

static IQueryable<T> ApplyRules<T>(
    this IQueryable<T> source, IEnumerable<Rule> rules)
{
    var predicate = PredicateBuilder.True<T>();

    var groups = rules.GroupBy(r => r.Field);

    foreach (var group in groups)
    {
        var groupPredicate = PredicateBuilder.False<T>();

        foreach (var rule in group)
        {
            groupPredicate = groupPredicate.Or(CreateRuleQuery(rule));
        }

        predicate = predicate.And(groupPredicate);
    }

    return source.Where(predicate.Expand());
}

用法类似于:

IQueryable<Person> source = …;

IQueryable<Person> result = source.ApplyRules(rules);

如果您在这些规则上使用它:

Name, eq, Peter
Name, eq, Paul
Age, ge, 18

然后谓词的主体将是(来自谓词的调试视图):

True && (False || $f.Name == "Peter" || $f.Name == "Paul") && (False || $f.Age >= 18)

所有这些Trues 和s 不应该是一个问题,但你可以通过稍微复杂一些False来摆脱它们。ApplyRules()

于 2013-02-11T19:18:16.753 回答
1

所以实际上很容易。

现在,您的代码会为每个规则生成一个 where,而您需要的是一个条件有点复杂的 where,因此需要对代码进行一些修改:

private static Expression GetComparisonExpression(this Rule rule, ParameterExpression parameter)
    {
        Expression property = Expression.Property(parameter, rule.Field);
        ConstantExpression constant = Expression.Constant(4);

        /* the code that generates constant and does some other stuff */

        switch (rule.Op)
        {
            case "eq": //Equals
            case "ne": //NotEquals
                {
                    var condition = rule.Op.Equals("eq")
                                        ? Expression.Equal(property, constant)
                                        : Expression.NotEqual(property, constant);
                    return condition;
                }

            default:
                throw new NotImplementedException();
        }
    }

这是原始代码所需内容的片段。此方法不会包装查询,而只是在给定参数上生成比较表达式与您的rule.

现在从生成查询的语句开始:

 var result = rules.Generate(_repository.All);

Generate 方法按属性名称对规则进行Field分组,并为每个组生成and also(这只是&&运算符)条件表达式:

(group1Comparision) && (group2Comparison) && so on


public static IQueryable<T> Generate<T>(this  IEnumerable<Rule> rules, IQueryable<T> query) where T : class
{
    if (rules.Count() == 0)
        return query;

    var groups = rules.GroupBy(x => x.Field).ToArray();

    var parameter = Expression.Parameter(typeof(T));
    var comparison = groups.First().GetComparisonForGroup(parameter);

    foreach (var group in groups.Skip(1))
    {
        var otherComparions = group.GetComparisonForGroup(parameter);
        comparison = Expression.AndAlso(comparison, otherComparions);
    }

    var lambda = Expression.Lambda(comparison, parameter);
    var call = Expression.Call(typeof(Queryable), "Where", new[] { query.ElementType }, query.Expression, lambda);
    return query.Provider.CreateQuery<T>(call);

}

请注意,按属性名称分组会使规则的原始顺序无关紧要。

最后一件事是为组创建比较||操作符:

public static Expression GetComparisonForGroup(this IEnumerable<Rule> group, ParameterExpression parameter)
{
        var comparison = group.Select((rule) => rule.GetComparisonExpression(parameter)).ToArray();

        return comparison.Skip(1).Aggregate(comparison.First(),
            (left, right) => Expression.OrElse(left, right));
}

因此,对于给定的规则列表,不需要外部库:

var rules = new Rule[]
            {
                new Rule{ Field = "A", Data = "4", Op="ne"},
                new Rule{ Field = "B", Data = "4", Op="eq"},
                new Rule{ Field = "A", Data = "4", Op="eq"},
                new Rule{ Field = "C", Data = "4", Op="ne"},
                new Rule{ Field = "A", Data = "4", Op="eq"},
                new Rule{ Field = "C", Data = "4", Op="eq"},
            };

我生成了这样的条件,该条件被引入到Where对您的查询的单次调用中:

($var1.A != 4 || $var1.A == 4 || $var1.A == 4) && $var1.B == 4 && ($var1.C != 4 || $var1.C == 4)
于 2013-02-11T19:40:13.940 回答
0

避免需要的 LINQKit 的替代方法.AsExpandable()是我ExpressionBuilder下面的课程。它Expression<Func<T,bool>>通过使用 And 或 Or 组合两个来创建一个新的 Expression 并使用纯Expression代码(没有 .Compile() 或类似的代码)来完成它。 我不记得其中一些想法的原始来源,但如果有人知道它们,我很乐意添加它们。

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;

public static class ExpressionBuilder
{
    public static Expression<Func<T, bool>> True<T>() { return f => true; }
    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    public static Expression<T> Compose<T>(this Expression<T> first, 
         Expression<T> second, 
         Func<Expression, Expression, Expression> merge)
    {
        // build parameter map (from parameters of second to parameters of first)
        var map = first.Parameters
                     .Select((f, i) => new { f, s = second.Parameters[i] })
                     .ToDictionary(p => p.s, p => p.f);

        // replace parameters in the second lambda expression with parameters from 
        // the first
        var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);

        // apply composition of lambda expression bodies to parameters from 
        // the first expression 
        return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(
        this Expression<Func<T, bool>> first,
        Expression<Func<T, bool>> second)
    {
        return first.Compose(second, Expression.And);
    }

    public static Expression<Func<T, bool>> Or<T>(
        this Expression<Func<T, bool>> first,
        Expression<Func<T, bool>> second)
    {
        return first.Compose(second, Expression.Or);
    }

    public class ParameterRebinder : ExpressionVisitor
    {
        private readonly Dictionary<ParameterExpression, ParameterExpression> map;

        public ParameterRebinder(
            Dictionary<ParameterExpression, 
            ParameterExpression> map)
        {
            this.map = map??new Dictionary<ParameterExpression,ParameterExpression>();
        }

        public static Expression ReplaceParameters(
            Dictionary<ParameterExpression, 
            ParameterExpression> map, 
            Expression exp)
        {
            return new ParameterRebinder(map).Visit(exp);
        }

        protected override Expression VisitParameter(ParameterExpression p)
        {
            ParameterExpression replacement;
            if (map.TryGetValue(p, out replacement))
            {
                p = replacement;
            }
            return base.VisitParameter(p);
        }
    }
}
于 2013-02-11T20:09:15.960 回答