• Lambda 表达式查询条件拼接


    public static class PredicateBuilder
        {
        

          /// <summary>
          /// 可以理解为创建一个初始化为True的Predicate。
          /// 注意:如果你是要创建一个OR组成的Predicate就不能把它初始化为True因为这样这个表达试永远为True了。
          /// </summary>
          /// <typeparam name="T"></typeparam>
          /// <returns></returns>

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

          /// <summary>
          /// 可以理解为创建一个初始化为False的Predicate。
          ///注意:如果你是要创建一个AND组成的Predicate就不能把它初始化为False因为这样这个表达试永远为False了。
          /// </summary>
          /// <typeparam name="T"></typeparam>
          /// <returns></returns>

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

    调用方法

      /*
           Lambda 表达式查询条件拼接
    
           调用实例
    
            var predicate = PredicateBuilder.True<Orders>();
            if (!string.IsNullOrWhiteSpace(select.so_text))
            {
                predicate = predicate.And(d => d.price > 10);
                predicate = predicate.And(d => d.price < 12);
            }
    
    
            var list = db.Orders.Where(predicate).ToList();

          全And:
          var where = PredicateBuilder.True<int>();
          where = where.And(x => x >= 50);
          where = where.And(x => x <= 70);
          var res = list.Where(where.Compile());

    
    

          全Or:
          var list = Enumerable.Range(1, 100);
          var where = PredicateBuilder.False<int>();
          where = where.Or(x => x == 50);
          where = where.Or(x => x == 70);
          var res = list.Where(where.Compile());

    
    

          var list = Enumerable.Range(1, 100);
          var where = PredicateBuilder.True<int>();
          where = where.And(x => x >= 50);
          where = where.And(x => x <= 70);
          var subwhere = PredicateBuilder.False<int>();
          subwhere = subwhere.Or(x => x == 60);
          subwhere = subwhere.Or(x => x == 61);
          where = where.And(subwhere);
          var res = list.Where(where.Compile());

    */
  • 相关阅读:
    1304. 和为零的N个唯一整数
    557. 反转字符串中的单词 III
    集群Eureka构建步骤
    单机Eureka构建步骤——08端口服务和8001端口服务注册进Eureka
    服务注册中心——Eureka基础知识
    存活的cloud
    系统中重复部分打包成一个jar包供其他工程使用(工程重构)
    cloud-consumer-order80微服务消费者订单Module模块
    cloud-provider-payment8001微服务提供者支付Module模块
    学习SpringCloud——项目工程搭建
  • 原文地址:https://www.cnblogs.com/OleRookie/p/5302626.html
Copyright © 2020-2023  润新知