public static void Show()
        {
            {
                Expression <Func <int, int, int> > exp = (m, n) => m * n + 2 + 3 + Get(m);
                // Change the expression exp to m*n -2
                OperationsVisitor visitor = new OperationsVisitor();
                Expression        expNew  = visitor.Modify(exp);
            }

            {
                ///Parse a lambda to a sql script, parse where condition
                ///ORM maps the database to program memory and manages the database by manipulating objects
                ///Developers don't need to know details of database.
                var source = new List <People>().AsQueryable();
                var result = source.Where <People>(p => p.Age > 5)
                             .Where(p => p.Name.Equals("124"))
                             .Where(p => p.Age < 10);
                //.ToList() //Only here execute the sql
                Expression <Func <People, bool> > lambda  = x => x.Age > 5;
                ConditionBuilderVisitor           visitor = new ConditionBuilderVisitor();
                visitor.Visit(lambda);
                Console.WriteLine(visitor.Condition());
            }

            {
                Expression <Func <People, bool> > lambda = x => x.Age > 5 && x.Id > 5 &&
                                                           x.Name.StartsWith("1") &&
                                                           x.Name.EndsWith("1") &&
                                                           x.Name.Contains("1");
                string sql = string.Format("Delete From [{0}] WHERE {1}", typeof(People).Name, "[Age]>5 AND [ID]>5");
                ConditionBuilderVisitor visitor = new ConditionBuilderVisitor();
                visitor.Visit(lambda);
                var result = visitor.Condition();
                Console.WriteLine(result);

                // Queryable is to parse linq to sql script
            }

            {
                Expression <Func <People, bool> > lambda  = x => x.Age > 5 && x.Name == "A" || x.Id > 5;
                Expression <Func <People, bool> > lambda1 = x => x.Age > 5 || (x.Name == "A" && x.Id > 5);
                Expression <Func <People, bool> > lambda2 = x => (x.Age > 5 || x.Name == "A") && x.Id > 5;

                ConditionBuilderVisitor vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
                vistor.Visit(lambda1);
                Console.WriteLine(vistor.Condition());
                vistor.Visit(lambda2);
                Console.WriteLine(vistor.Condition());
            }

            {
                //Link expression
                Expression <Func <People, bool> > lambda1 = x => x.Age > 5;
                Expression <Func <People, bool> > lambda2 = x => x.Id > 5;
            }
        }
Esempio n. 2
0
        public static void Show()
        {
            {
                //修改表达式目录树
                Expression <Func <int, int, int> > exp = (m, n) => m * n + 2;

                OperationsVisitor visitor = new OperationsVisitor();
                //visitor.Visit(exp);
                Expression expNew = visitor.Modify(exp);
            }

            {
                Expression <Func <People, bool> > lambda = x => x.Age > 5 && x.Id > 5 &&
                                                           x.Name.StartsWith("1") &&
                                                           x.Name.EndsWith("1") &&
                                                           x.Name.Contains("1");

                string sql = string.Format("Delete From [{0}] WHERE {1}"
                                           , typeof(People).Name
                                           , " [Age]>5 AND [ID] >5"
                                           );
                ConditionBuilderVisitor vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }
            {
                Expression <Func <People, bool> > lambda = x => x.Age > 5 && x.Name == "A" || x.Id > 5;
                ConditionBuilderVisitor           vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }
            {
                Expression <Func <People, bool> > lambda = x => x.Age > 5 || (x.Name == "A" && x.Id > 5);
                ConditionBuilderVisitor           vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }
            {
                Expression <Func <People, bool> > lambda = x => (x.Age > 5 || x.Name == "A") && x.Id > 5;
                ConditionBuilderVisitor           vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }

            #region 表达式链接
            {
                Expression <Func <People, bool> > lambda1 = x => x.Age > 5;
                Expression <Func <People, bool> > lambda2 = x => x.Id > 5;
                Expression <Func <People, bool> > lambda3 = lambda1.And(lambda2);
                Expression <Func <People, bool> > lambda4 = lambda1.Or(lambda2);
                Expression <Func <People, bool> > lambda5 = lambda1.Not();
                Do1(lambda3);
                Do1(lambda4);
                Do1(lambda5);
            }
            #endregion
        }
Esempio n. 3
0
        public static void Show()
        {
            {
                Expression <Func <int, int, int> > exp = (m, n) => m * n + 2;

                OperationsVisitor visitor    = new OperationsVisitor();
                Expression        exp1       = visitor.Visit(exp);
                Expression        expression = visitor.Modify(exp);
            }

            {
                Expression <Func <People, bool> > exp  = (p) => p.Age > 5 && p.Id > 5 && p.Name.StartsWith("t");
                ConditionBuilderVisitor           cond = new ConditionBuilderVisitor();
                cond.Visit(exp);
                string sResult = cond.Condition();
            }
        }
        public static void Show()
        {
            {
                //修改表达式目录树
                Expression <Func <int, int, int> > exp = (m, n) => m * n + 2 + 3 + Get(m);
                //我想修改下,变成m*n-2
                OperationsVisitor visitor = new OperationsVisitor();
                //visitor.Visit(exp);
                Expression expNew = visitor.Modify(exp);
            }
            {
                var source = new List <People>().AsQueryable();//DbSet
                var result = source.Where <People>(p => p.Age > 5)
                             .Where(p => p.Name.Equals("123"))
                             .Where(p => p.Age < 10)
                     //.ToList()
                ;    //SELECT * FROM People Where Age> '5'

                Expression <Func <People, bool> > lambda = x => x.Age > 5;

                ConditionBuilderVisitor vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }


            {
                //ORM 把数据库映射到程序内存  通过操作对象来完成对数据库的管理
                //屏蔽数据库,开发者完全不需要知道

                //Linq To Object   实现IEnumerable
                //Enumerable
                //Queryable
                //IQueryable

                Expression <Func <People, bool> > lambda = x => x.Age > 5 && x.Id > 5 &&
                                                           x.Name.StartsWith("1") &&
                                                           x.Name.EndsWith("1") &&
                                                           x.Name.Contains("1");

                string sql = string.Format("Delete From [{0}] WHERE {1}"
                                           , typeof(People).Name
                                           , " [Age]>5 AND [ID] >5"
                                           );
                ConditionBuilderVisitor vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }
            {
                Expression <Func <People, bool> > lambda = x => x.Age > 5 && x.Name == "A" || x.Id > 5;
                ConditionBuilderVisitor           vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }
            {
                Expression <Func <People, bool> > lambda = x => x.Age > 5 || (x.Name == "A" && x.Id > 5);
                ConditionBuilderVisitor           vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }
            {
                Expression <Func <People, bool> > lambda = x => (x.Age > 5 || x.Name == "A") && x.Id > 5;
                ConditionBuilderVisitor           vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }

            #region 表达式链接
            {
                Expression <Func <People, bool> > lambda1 = x => x.Age > 5;
                Expression <Func <People, bool> > lambda2 = x => x.Id > 5;
                Expression <Func <People, bool> > lambda3 = lambda1.And(lambda2);
                Expression <Func <People, bool> > lambda4 = lambda1.Or(lambda2);
                Expression <Func <People, bool> > lambda5 = lambda1.Not();
                Do1(lambda3);
                Do1(lambda4);
                Do1(lambda5);
            }
            #endregion
        }
Esempio n. 5
0
        //表达式目录树的拼装和拆解作为本周的家庭作业

        public static void Show()
        {
            //ExpressionVisitor访问者类,Visit是一个入口,先判断,进一步的解析,然后
            //1、lambada会区分参数和方法体,调度到更加专业的方法中解析;
            //2、根据表达式的类型,调度到更加专业的方法中解析;
            //3、默认根据旧的模式产生一个新的表达式目录树;

            // 解析表达式目录树的时候,先从Visit进入,调度更加专业的方法进一步解析;

            {
                //修改表达式目录树
                Expression <Func <int, int, int> > exp = (m, n) => m * n + 2;
                OperationsVisitor visitor = new OperationsVisitor();
                //visitor.Visit(exp);
                Expression expNew = visitor.Modify(exp);
            }
            {
                // 写成这样的表达式目录树怎么变成sql呢?
                Expression <Func <People, bool> > lambda = x => x.Age > 5;
                // select * from People where Age>5
                new List <People>().AsQueryable().Where(x => x.Age > 5 && x.Id == 10);

                ConditionBuilderVisitor vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }

            Console.WriteLine("更加复杂一点");
            {
                //select * from People where Age>5 and Id>5
                Expression <Func <People, bool> > lambda = x => x.Age > 5 &&
                                                           x.Id > 5 &&
                                                           x.Name.StartsWith("1") &&
                                                           x.Name.EndsWith("1") &&
                                                           x.Name.Contains("1");

                string sql = string.Format("Delete From [{0}] WHERE {1}"
                                           , typeof(People).Name
                                           , " [Age]>5 AND [ID] >5"
                                           );
                ConditionBuilderVisitor vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }
            {
                //且或非
                Expression <Func <People, bool> > lambda = x => x.Age > 5 && x.Name == "A" || x.Id > 5;
                ConditionBuilderVisitor           vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }
            {
                Expression <Func <People, bool> > lambda = x => x.Age > 5 || (x.Name == "A" && x.Id > 5);
                ConditionBuilderVisitor           vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }
            {
                Expression <Func <People, bool> > lambda = x => (x.Age > 5 || x.Name == "A") && x.Id > 5;
                ConditionBuilderVisitor           vistor = new ConditionBuilderVisitor();
                vistor.Visit(lambda);
                Console.WriteLine(vistor.Condition());
            }

            #region 表达式链接
            {
                // 把多个表达式拼接起来
                Expression <Func <People, bool> > lambda1 = x => x.Age > 5;
                Expression <Func <People, bool> > lambda2 = x => x.Id > 5;
                Expression <Func <People, bool> > lambda3 = lambda1.And(lambda2);
                Expression <Func <People, bool> > lambda4 = lambda1.Or(lambda2);
                Expression <Func <People, bool> > lambda5 = lambda1.Not();
                Do1(lambda3);
                Do1(lambda4);
                Do1(lambda5);
            }
            #endregion
        }