• C#动态拼接lambda表达式


    C#动态拼接lambda表达式

    拼接帮助类

      #region 动态拼接lambda表达式
    
        /// 
        /// Lambda表达式拼接扩展类
        /// 
        public static class Utility
        {
            /// 
            /// Lambda表达式拼接
            /// 
            /// 
            /// 
            /// 
            /// 
            /// 
            public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
            {
                // 构建参数映射(从第二个参数到第一个参数)
                var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
                // 将第二个lambda表达式中的参数替换为第一个lambda表达式的参数
                var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
                // 将lambda表达式体的组合应用于第一个表达式中的参数
                return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
            }
            /// 
            /// and扩展
            /// 
            /// 
            /// 
            /// 
            /// 
            public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
            {
                return first.Compose(second, Expression.AndAlso);
            }
            /// 
            /// or扩展
            /// 
            /// 
            /// 
            /// 
            /// 
            public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
            {
                return first.Compose(second, Expression.OrElse);
            }
        }
        /// 
        /// 
        /// 
        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);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    调用方法:

     Expression<Func<OrderDO, bool>> expression=t=>true;
    
                expression=expression.And(t => t.orderStartTime > start && t.orderStartTime < end);
                if (!string.IsNullOrWhiteSpace(orderid))
                {
                    expression= expression.And(t => t.OrderID == orderid);
                }  
                if (!string.IsNullOrWhiteSpace(BatchID))
                {
                    expression= expression.And(t => t.BatchNo == BatchID);
                } 
                if(!string.IsNullOrWhiteSpace(deliver))
                {
                    expression= expression.And(t => t.deliveryBillNo == deliver);
                } 
                if(!string.IsNullOrWhiteSpace(UserTel))
                {
                    expression= expression.And(t => t.buyerMobile == UserTel);
                }
                if(string.IsNullOrWhiteSpace(orderid)&&string.IsNullOrWhiteSpace(BatchID)&&string.IsNullOrWhiteSpace(deliver)&&string.IsNullOrWhiteSpace(UserTel))
                {
                    expression= expression.And(t => (orderstatus == t.ApproveStatus || orderstatus == 0)&& (supplierid == t.supplierid || supplierid == 0)&& (ordertype == t.orderType || ordertype == 0));
                }
                orders= SqlsugarHelper.Init().GetClient().Queryable<OrderDO>().AS("Erp_Order_Info")
                            .Where(expression).OrderByDescending(item => item.orderStartTime)
                    .ToPageList(page, limit, ref totalcount);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    C语言题收录(四)
    安装docker-compose
    递归&回溯&剪枝-括号生成
    <Linux> 基础IO
    1.4.21 实验21:vrrp主备
    pip install mysqlclient报错
    Labelme分割标注软件
    线性代数 --- 矩阵的QR分解,A=QR
    读《基于深度学习的跨视角步态识别算法研究》
    @Elasticsearch之深度应用及原理剖析--分布式数据一致性机制
  • 原文地址:https://blog.csdn.net/qq_42455262/article/details/126386209