如何在.NET中创建表达式生成器

时间:2019-02-14 19:03:25

标签: c# .net linq iqueryable linq-expressions

我写了一些代码来过滤我们网站上的产品,并且我得到了非常难闻的代码气味。用户可以从这些过滤器中选择1- *,这意味着我需要具体说明WHERE子句。

我认为我正在寻找一种构建lambda表达式的方法,因此对于每个过滤器,我都可以“修改”我的WHERE子句-但我不确定如何在.NET中做到这一点。必须是一种方法。

处于当前状态的代码(有效地硬编码,不是动态的,添加更多过滤器选项会很麻烦。

public static class AgeGroups
{
    public static Dictionary<string, int> Items = new Dictionary<string, int>(){
        { "Modern (Less than 10 years old)", 1 },
        { "Retro (10 - 20 years old)", 2 },
        { "Vintage(20 - 70 years old)", 3 },
        { "Antique(70+ years old)", 4 }
    };

    public static IQueryable<ProductDTO> FilterAgeByGroup(IQueryable<ProductDTO> query, List<string> filters)
    {
        var values = new List<int>();
        var currentYear = DateTime.UtcNow.Year;
        foreach (var key in filters)
        {
            var matchingValue = Items.TryGetValue(key, out int value);

            if (matchingValue)
            {
                values.Add(value);
            }
        }

        if (Utility.EqualsIgnoringOrder(values, new List<int> { 1 }))
        {
            query = query.Where(x => x.YearManufactured >= currentYear - 10);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 2 }))
        {
            query = query.Where(x => x.YearManufactured <= currentYear - 10 && x.YearManufactured >= currentYear - 20);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 3 }))
        {
            query = query.Where(x => x.YearManufactured <= currentYear - 20 && x.YearManufactured >= currentYear - 70);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 4 }))
        {
            query = query.Where(x => x.YearManufactured <= currentYear - 70);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 1, 2}))
        {
            query = query.Where(x => x.YearManufactured >= currentYear - 20);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 1, 3 }))
        {
            query = query.Where(x => x.YearManufactured >= currentYear - 10 || (x.YearManufactured <= currentYear - 20 && x.YearManufactured >= currentYear - 70));
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 1, 4 }))
        {
            query = query.Where(x => x.YearManufactured >= currentYear - 10 ||  x.YearManufactured <= currentYear - 70);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 2, 3 }))
        {
            query = query.Where(x => x.YearManufactured <= currentYear - 10 && x.YearManufactured >= currentYear - 70);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 2, 4 }))
        {
            query = query.Where(x => (x.YearManufactured <= currentYear - 10 && x.YearManufactured >= currentYear - 20) 
                                     || x.YearManufactured <= currentYear - 70);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 1, 2, 3 }))
        {
            query = query.Where(x => x.YearManufactured >= currentYear - 70);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 1, 2, 4 }))
        {
            query = query.Where(x => x.YearManufactured >= currentYear - 20 || x.YearManufactured <= currentYear - 70);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 2, 3, 4}))
        {
            query = query.Where(x => x.YearManufactured <= currentYear - 10);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 1, 3, 4}))
        {
            query = query.Where(x => x.YearManufactured >= currentYear - 10 || x.YearManufactured <= 20);
        }
        else if (Utility.EqualsIgnoringOrder(values, new List<int> { 1, 2, 3, 4 }))
        {
            // all
        }
        return query;
    }
}

4 个答案:

答案 0 :(得分:3)

您似乎面临着组合爆炸。您可以使用修改后的Items集合静态声明一些简单案例:

static Dictionary<string, Expression<Func<int, int, bool>>> Items
   = new Dictionary<string, Expression<Func<int, int, bool>>>
{
    {
      "Modern (Less than 10 years old)",
      (yearManufactured, currentYear) => yearManufactured >= currentYear - 10
    },
    {
      "Retro (10 - 20 years old)",
      (yearManufactured, currentYear) => yearManufactured <= currentYear - 10 && yearManufactured >= currentYear - 20
    },
    {
      "Vintage(20 - 70 years old)",
      (yearManufactured, currentYear) => yearManufactured <= currentYear - 20 && yearManufactured >= currentYear - 70
    },
    {
      "Antique(70+ years old)",
      (yearManufactured, currentYear) => yearManufactured <= currentYear - 70
    }
};

现在,您可以将简单案例与Linq Expression OrElse动态结合起来。尝试以下代码:

public static IQueryable<ProductDTO> FilterAgeByGroup(
  IQueryable<ProductDTO> query, List<string> filters)
{
    var conditions = new List<Expression>();
    foreach (var key in filters)
        if (Items.TryGetValue(key, out Expression<Func<int, int, bool>> value))
            conditions.Add(value);

    // return as is if there no conditions
    if (!conditions.Any())
        return query;

    var x = Expression.Parameter(typeof(ProductDTO), "x");
    var yearManufactured = Expression.PropertyOrField(x, "YearManufactured");
    var currentYear = Expression.Constant(DateTime.UtcNow.Year);
    var body = conditions.Aggregate(
        (Expression) Expression.Constant(false), // ignore item by default
        (c, n) => Expression.OrElse(c, Expression.Invoke(n, yearManufactured, currentYear)));

    var lambda = Expression.Lambda<Func<ProductDTO, bool>>(body, x);
    return query.Where(lambda);
}

答案 1 :(得分:3)

我最近亲自遇到了这个问题。通过关于SO的另一个问题,我找到了http://www.albahari.com/nutshell/predicatebuilder.aspx。基本上,您想构建一个谓词并将其传递到查询的where子句中。

public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> where1, 
     Expression<Func<T, bool>> where2)
{
    InvocationExpression invocationExpression = Expression.Invoke(where2, 
         where1.Parameters.Cast<Expression>());
    return Expression.Lambda<Func<T, bool>>(Expression.OrElse(where1.Body, 
         invocationExpression), where1.Parameters);
}

public static IQueryable<ProductDTO> FilterAgeByGroup(IQueryable<ProductDTO> query,  
   List<string> filters, int currentYear)
{
    var values = new HashSet<int>();
    //Default value
    Expression<Func<ProductDTO, bool>> predicate = (ProductDTO) => false;

    foreach (var key in filters)
    {
        var matchingValue = Items.TryGetValue(key, out int value);

        if (matchingValue)
        {
            values.Add(value);
        }
    }

    if (values.Count == 0)
        return query;

    if (values.Contains(1))
    {
        predicate = predicate.Or(x => x.YearManufactured >= currentYear - 10);
    }

    if (values.Contains(2))
    {
        predicate = predicate.Or(x => x.YearManufactured <= currentYear - 10 && 
            x.YearManufactured >= currentYear - 20);
    }

    if (values.Contains(3))
    {
        predicate = predicate.Or(x => x.YearManufactured <= currentYear - 20 && 
            x.YearManufactured >= currentYear - 70);
    }

    if (values.Contains(4))
    {
        predicate = predicate.Or(x => x.YearManufactured <= currentYear - 70);
    }

    return query.Where(predicate);
}

答案 2 :(得分:1)

使用LINQKit,您可以轻松组合谓词。另外,没有理由将filters List转换为另一个List只是为了处理它们,只要您可以合并它们,只需添加传入的每个过滤器即可。

public static class AgeGroups {
    public static Dictionary<string, int> Items = new Dictionary<string, int>(){
        { "Modern (Less than 10 years old)", 1 },
        { "Retro (10 - 20 years old)", 2 },
        { "Vintage(20 - 70 years old)", 3 },
        { "Antique(70+ years old)", 4 }
    };

    public static IQueryable<ProductDTO> FilterAgeByGroup(IQueryable<ProductDTO> query, List<string> filters) {
        var currentYear = DateTime.UtcNow.Year;

        var pred = PredicateBuilder.New<ProductDTO>();
        foreach (var fs in filters) {
            if (Items.TryGetValue(fs, out var fv)) {
                switch (fv) {
                    case 1:
                        pred = pred.Or(p => currentYear-p.YearManufactured < 10);
                        break;
                    case 2:
                        pred = pred.Or(p => 10 <= currentYear-p.YearManufactured && currentYear-p.YearManufactured  <= 20);
                        break;
                    case 3:
                        pred = pred.Or(p => 20 <= currentYear-p.YearManufactured && currentYear-p.YearManufactured  <= 70);
                        break;
                    case 4:
                        pred = pred.Or(p => 70 <= currentYear-p.YearManufactured);
                        break;
                }
            }
        }

        return query.Where(pred);
    }
}

答案 3 :(得分:1)

我建议动态构造要传递给Where的表达式(如AlexAndreev's answer;但不使用任何编译器生成的表达式,只有{{1}中的工厂方法}。

首先使用每个条件的最小和最大年龄值元组定义原始字典:

System.Linq.Expressions.Expression

然后,您可以动态构建谓词,并根据传入的过滤器及其匹配条件添加条件:

// using static System.Linq.Expressions.Expression

public static Dictionary<string, (int code, int? min, int? max)> Items = new Dictionary<string, (int code, int? min, int? max)>(){
    { "Modern (Less than 10 years old)", (1, null, 10) },
    { "Retro (10 - 20 years old)", (2, 10, 20) },
    { "Vintage(20 - 70 years old)", (3, 20, 70) },
    { "Antique(70+ years old)", (4, 70, null) }
};