动态拼接Lambda表达式-表达式目录树动态构建及修改-----表达式树的访问过程,并转化成sql语句

代码如下:

 #region 表达式树的访问过程,并转化成sql语句
        /// <summary>
        /// 访问 表达式树 Expression<Func<MyClass, bool>> expressionFunc = x => x.Age > 5 &&  x.Id == 8;
        /// 并转化成sql语句 select * from MyClass where age > 5 and id = 8
        /// </summary>
        public class OperatorExpressionToSql : ExpressionVisitor
        {
            /// <summary>
            /// 存放Expression表达式树的内容
            /// </summary>
            public Stack<string> StackSet { get; set; }


            public OperatorExpressionToSql()
            {
                StackSet = new Stack<string>();
                StackSet.Push("(");
            }
            /// <summary>
            /// 修改表达式树的形式
            /// </summary>
            /// <param name="expression"></param>
            /// <returns></returns>
            public Expression Modify(Expression expression)
            {
                //base.Visit(expression);
                //if (expression is BinaryExpression binary)
                //{
                //    if (binary.NodeType == ExpressionType.Add)
                //    {
                //        var left = base.Visit(binary.Left); ;
                //        var right = base.Visit(binary.Right);
                //        var result = Expression.Subtract(left, right);
                //        return result;
                //    }
                //}
                return base.Visit(expression);
            }

            /// <summary>
            /// 表达式树的二元操作
            /// </summary>
            /// <param name="node"></param>
            /// <returns></returns>
            protected override Expression VisitBinary(BinaryExpression node)
            {
                if (node.NodeType == ExpressionType.AndAlso)
                {
                    base.Visit(node.Left);
                    StackSet.Push("and");
                    base.Visit(node.Right);
                    //var result = Expression.Subtract(left, right);
                    //return result;
                    StackSet.Push(")");
                }
                else if (node.NodeType == ExpressionType.GreaterThan)
                {
                    if (node.Left is MemberExpression member)
                    {
                        StackSet.Push(member.Member.Name);
                    }
                    StackSet.Push(">");
                    if (node.Right is ConstantExpression constant)
                    {
                        StackSet.Push(constant.Value.ToString());
                    }
                }
                else if (node.NodeType == ExpressionType.Equal)
                {
                    if (node.Left is MemberExpression member)
                    {
                        StackSet.Push(member.Member.Name);
                    }
                    StackSet.Push("=");
                    if (node.Right is ConstantExpression constant)
                    {
                        StackSet.Push(constant.Value.ToString());
                    }
                }

                //StackSet.Push(node.Value.ToString());
                return node;
                //return base.VisitBinary(node);
            }

            /// <summary>
            /// 表达式树的常量操作
            /// </summary>
            /// <param name="node"></param>
            /// <returns></returns>
            protected override Expression VisitConstant(ConstantExpression node)
            {
                StackSet.Push(node.Value.ToString());
                return base.VisitConstant(node);
            }
        }

        /// <summary>
        /// 测试表达式树的访问过程,并转化成sql语句
        /// </summary>
        public static void TestDynamicExpressionToSql()
        {
            //访问 表达式树 
            Expression<Func<MyClass, bool>> expressionFunc = x => x.Age > 5 && x.Name == "8";
            /// 并转化成sql语句 select * from MyClass where age > 5 and id = 8
            OperatorExpressionToSql visitor = new OperatorExpressionToSql();
            var expression = visitor.Modify(expressionFunc.Body);
            var d = string.Join(' ', visitor.StackSet.Reverse().ToArray());
            //while (visitor.StackSet.Count > 0)
            //{
            //    Console.WriteLine($"结果:{visitor.StackSet.Pop()}");
            //}
            Console.WriteLine($"结果:{d}");
        }
        #endregion

 

posted @ 2020-12-02 17:32  龙骑科技  阅读(252)  评论(0编辑  收藏  举报