示例#1
0
        public static string Stringify(Expression node)
        {
            var printer = new SimpleExpressionPrinter();

            printer.Visit(node);
            return(printer._sb.ToString().Trim());
        }
        public void Should_Remove_Null_Propgation_In_MethodCall_Expressions()
        {
            List <Product> productList = new List <Product>()
            {
                new Product()
                {
                    Name = "Product 1"
                },
                new Product()
                {
                    Name = null
                }
            };

            var productsQueryable = productList.AsQueryable();

            var withPropagation = (from p in productsQueryable
                                   where (p.Name == null ? "product 1" : p.Name.ToLower()) == "product 1"
                                   select p).Expression;

            var withoutPropagation = (from p in productsQueryable
                                      where p.Name.ToLower() == "product 1"
                                      select p).Expression;

            var propagationRemoved = new MethodCallNullPropagationYanker().Visit(withPropagation);

            string withPropagationString    = SimpleExpressionPrinter.Stringify(withPropagation);
            string withoutPropagationString = SimpleExpressionPrinter.Stringify(withoutPropagation);
            string propagationRemovedString = SimpleExpressionPrinter.Stringify(propagationRemoved);

            Assert.NotEqual(withPropagationString, propagationRemovedString);
            Assert.Equal(withoutPropagationString, propagationRemovedString);
        }
        public void Should_Remove_Null_Propgation_In_MethodCall_Expressions()
        {
            var storeList = new List <Store>
            {
                new Store {
                    Name = "Store 1"
                },
            };

            var productList = new List <Product>()
            {
                new Product {
                    Name = "Product 1", Stores = storeList
                },
            };

            var productsQueryable = productList.AsQueryable();

            var withPropagation = (from p in productsQueryable
                                   where (p.Stores == null ? Enumerable.Empty <Store>() : p.Stores).Any(x => x.Name == "Store 1")
                                   select p).Expression;

            var withoutPropagation = (from p in productsQueryable
                                      where (p.Stores.Any(x => x.Name == "Store 1"))
                                      select p).Expression;

            var propagationRemoved = new EnumerableNullPropagationYanker().Visit(withPropagation);

            string withPropagationString    = SimpleExpressionPrinter.Stringify(withPropagation);
            string withoutPropagationString = SimpleExpressionPrinter.Stringify(withoutPropagation);
            string propagationRemovedString = SimpleExpressionPrinter.Stringify(propagationRemoved);

            Assert.NotEqual(withPropagationString, propagationRemovedString);
            Assert.Equal(withoutPropagationString, propagationRemovedString);
        }