public void TestUnion()
        {
            const int resultRowCount = 5;
            // create QueryDesigner with ElementType == Products
            var queryDesinger = new QueryDesigner(context, typeof(Products));

            //create root node which elementType has the same type in queryDesigner
            var root = new JoinNode(typeof(Products));

            // create child node Categories with propertyName "Products".
            // Because Categories linked with Products by next property:
            // public EntitySet<Products> Products
            var categoryNode = new JoinNode(typeof(Categories), "Category", "Products");

            // add categoryNode to root node
            root.AddChildren(categoryNode);

            // create filter by Products.ProductName like "%l%"
            var productNameCondition = new Condition("ProductName", "l", ConditionOperator.Like);
            root.AddConditions(productNameCondition);

            // create filter by Categories.Description like "Sweet%"
            var categoryNameCondition = new Condition("Description", "Sweet", ConditionOperator.StartsWith, typeof(Categories));
            categoryNode.AddConditions(categoryNameCondition);

            // create ordering, because we call Skip and Take method.
            Ordering order = new Ordering("ProductName");

            // make join Products table with Categories filtered by conditions
            // and ordered by already created ordering
            queryDesinger.Join(root, new OrderingList(order));

            QueryDesigner cloneQueryDesigner = (QueryDesigner)queryDesinger.Clone();

            queryDesinger.Skip(0).Take(3);
            cloneQueryDesigner.Skip(2).Take(4);

            queryDesinger.Union(cloneQueryDesigner);

            var list = new List<Products>(queryDesinger.Cast<Products>());
            Assert.AreEqual(resultRowCount, list.Count);
        }
        public void TestLeftOuterJoin()
        {
            //            var res = (from product in context.Products
            //                       orderby product.ProductID
            //                       select new { product, Order_Details = product.Order_Details.Where(p => p.Quantity > 120), Order_Detail = product.Suppliers });
            //            var resutl = res.ToList();
            ////            var res = (from categories in context.Categories
            ////                       orderby categories.CategoryID
            ////                       select new { categories, Products = categories.Products.Where(p => p.ProductName.Contains("l")).Select(p=>p.Order_Details) });
            ////            var resutl = res.ToList();
            //
            //            foreach (var list1 in resutl)
            //            {
            //                var value = list1.product.Order_Details.FirstOrDefault();
            //                if (value != null)
            //                {
            //                    Console.WriteLine(value.Quantity);
            //                }
            //            }

            const int quantity = 120;

            var root = new JoinNode(typeof(Products));
            var orderDetailNode = new JoinNode(typeof(Order_Details), "Order_Details", "Product", JoinType.LeftOuterJoin);
            root.AddChildren(orderDetailNode);

            // add condition for filtering by CategoryName == "Condiments"
            orderDetailNode.AddConditions(new Condition("Quantity", quantity, ConditionOperator.GreaterThan));

               var supplierNode = new JoinNode(typeof(Suppliers), "Suppliers", "Products", JoinType.LeftOuterJoin);

            root.AddChildren(supplierNode);

            var orderNode = new JoinNode(typeof(Orders), "Orders", "Order_Details", JoinType.LeftOuterJoin);
            orderDetailNode.AddChildren(orderNode);

            var queryDesinger = new QueryDesigner(context, root);

            var query = queryDesinger;
            foreach (Products value in query)
            {
                var orderDetails = value.Order_Details.FirstOrDefault();
                if (orderDetails != null)
                {
                    Console.WriteLine(orderDetails.Quantity + " " + orderDetails.Orders.ShipName);
                }
            }
        }
        public void TestJoinWithOneChildAndFilteredByProductNameAndCategoryNameV2()
        {
            const string productName = "Louisiana";
            const string categoryName = "Condiments";
            const int resultRowCount = 2;
            //create root node
            var root = new JoinNode(typeof(Products));

            // add condition for filtering by ProductName Like "Louisiana%"
            root.AddConditions(new Condition("ProductName", productName, ConditionOperator.StartsWith));

            // add child node Categories with propertyName "Products".
            // Because Categories linked with Products by next property:
            // public EntitySet<Products> Products
            var categoryNode = new JoinNode(typeof(Categories), "Category", "Products");
            root.AddChildren(categoryNode);

            // add condition for filtering by CategoryName == "Condiments"
            categoryNode.AddConditions(new Condition("CategoryName", categoryName));

            var queryDesinger = new QueryDesigner(context, root);
            var list = new List<Products>(queryDesinger.Cast<Products>());
            Assert.AreEqual(resultRowCount, list.Count);

            string query =
                @"SELECT ProductID FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID
            WHERE Products.ProductName like N'Louisiana%' AND Categories.CategoryName = N'Condiments'";

            CheckDataWithExecuteReaderResult(query, resultRowCount, list);
        }
        public void TestFirstOrDefault()
        {
            // create QueryDesigner with ElementType == Products
            var queryDesinger = new QueryDesigner(context, typeof(Products));

            //create root node which elementType has the same type in queryDesigner
            var root = new JoinNode(typeof(Products));

            // create child node Categories with propertyName "Products".
            // Because Categories linked with Products by next property:
            // public EntitySet<Products> Products
            var categoryNode = new JoinNode(typeof(Categories), "Category", "Products");

            // add categoryNode to root node
            root.AddChildren(categoryNode);

            // create filter by Products.ProductName like "%l%"
            var productNameCondition = new Condition("ProductName", "l", ConditionOperator.Like);
            root.AddConditions(productNameCondition);

            // create filter by Categories.Description like "Sweet%"
            var categoryNameCondition = new Condition("Description", "Sweet", ConditionOperator.StartsWith, typeof(Categories));
            categoryNode.AddConditions(categoryNameCondition);

            // make join Products table with Categories filtered by conditions
            // and ordered by already created ordering
            queryDesinger.Join(root);

            object first = queryDesinger.FirstOrDefault();

            Assert.IsInstanceOfType(typeof(Products), first);
        }
        public void TestComplicatedLeftOuterJoin()
        {
            const int quantity = 120;

            var root = new JoinNode(typeof(Customers));
            var ordersNode = new JoinNode(typeof(Orders), "Orders", "Customers", JoinType.LeftOuterJoin);
            root.AddChildren(ordersNode);
            root.AddConditions(new Condition("Address", "Kirchgasse 6"));

            var orderDetailNode = new JoinNode(typeof(Order_Details), "Order_Details", "Orders", JoinType.LeftOuterJoin);
            orderDetailNode.AddConditions(new Condition("Quantity", quantity, ConditionOperator.GreaterThan));

            ordersNode.AddChildren(orderDetailNode);

            var productNode = new JoinNode(typeof(Products), "Products", "Order_Details", JoinType.LeftOuterJoin);
            orderDetailNode.AddChildren(productNode);

            var employeesNode = new JoinNode(typeof(Employees), "Employees", "Orders", JoinType.LeftOuterJoin);
            ordersNode.AddChildren(employeesNode);

            var shippersNode = new JoinNode(typeof(Shippers), "Shippers", "Orders", JoinType.LeftOuterJoin);
            ordersNode.AddChildren(shippersNode);

            var queryDesinger = new QueryDesigner(context, root);

            var query = queryDesinger;
            foreach (Customers value in query)
            {
                var orders = value.Orders.Where(o => o.Order_Details.Count > 0).FirstOrDefault();
                if (orders != null)
                {
                    Console.WriteLine("ShipName: " + orders.ShipName);
                    foreach (var orderDetail in orders.Order_Details)
                    {
                        Console.WriteLine("ProductName: " + orderDetail.Products.ProductName);
                    }
                }
            }
        }
        public void TestLeftOuterJoin()
        {
            var query = from orderDetail in context.Order_Details
                        join product in context.Products on orderDetail.Product equals product into gr
                        from grProduct in gr.DefaultIfEmpty()
                        where grProduct.ProductName == "some"
                        select new {orderDetail, pr = grProduct};
            query.ToList();

            var ordeDetailrNode = new JoinNode(typeof (Order_Detail));
            var productNode = new JoinNode(typeof(Product), "Product", "Order_Details", JoinType.LeftOuterJoin);
            productNode.AddConditions(new Condition("ProductName", "some"));
            ordeDetailrNode.AddChildren(productNode);

            var queryDesigner = new QueryDesigner(context, ordeDetailrNode).Cast<Order_Detail>().ToList();
            //            var dlo = new DataLoadOptions();
            //            dlo.AssociateWith<Category>(c => c.Products.Where(p => p.ProductName.Contains("l")));
            //            context.LoadOptions = dlo;
            //            var res1 = (from category in context.Categories
            //                        select category);
            //            foreach (var re in res1)
            //            {
            //                Console.WriteLine(re.CategoryID + " " + re.Products.Count);
            //            }

            //            var res = (from product in context.Products
            //                       orderby product.ProductID
            //                       select new { product, product.Supplier,
            //                                    Order_Details = product.Order_Details.Where(p => p.Quantity > 120)
            //                                        .Select(od => new { Order_Detail = od, Order = od.Order })
            //                       });
            //
            //            var queryable = res.ToList();
            //            foreach (var list in queryable)
            //            {
            //                list.product.Order_Details.SetSource(list.Order_Details.Select(od=>od.Order_Detail));
            ////                foreach (var detail in list.Order_Details)
            ////                {
            ////                    detail.Product = list.product;
            ////                }
            //            }
            //            foreach (var list in queryable)
            //            {
            //                var value = list.product.Order_Details.FirstOrDefault();
            //                if (value != null)
            //                {
            //                    Console.WriteLine(value.Quantity + " " + value.Order.ShipCity);
            //                }
            //            }
            //            return;
            //            const int quantity = 120;
            //
            //            var root = new JoinNode(typeof(Product));
            //            var orderDetailNode = new JoinNode(typeof(Order_Detail), "Order_Details", "Product", JoinType.LeftOuterJoin);
            //            root.AddChildren(orderDetailNode);
            //
            //            // add condition for filtering by CategoryName == "Condiments"
            //            orderDetailNode.AddConditions(new Condition("Quantity", quantity, ConditionOperator.GreaterThan));
            //
            //            var orderNode = new JoinNode(typeof (Order), "Order", "Order_Details", JoinType.LeftOuterJoin);
            //            orderDetailNode.AddChildren(orderNode);
            //
            //            var supplierNode = new JoinNode(typeof(Supplier), "Supplier", "Products", JoinType.LeftOuterJoin);
            //
            //            root.AddChildren(supplierNode);
            //
            //            var queryDesinger = new QueryDesigner(context, root);
            //
            //            var result = queryDesinger.Cast<Product>().ToList();
            //            foreach (Product list1 in result)
            //            {
            ////                Console.WriteLine(list1.product.Supplier.CompanyName);
            ////                if (list1.product.Order_Details.HasLoadedOrAssignedValues)
            //                {
            //                    var value = list1.Order_Details.FirstOrDefault();
            //                    if (value != null)
            //                    {
            //                        Console.WriteLine(value.Quantity + " " + value.Order.ShipCity);
            //                    }
            //                }
            //            }

            //            var res = from categories in context.Categories
            //                      join product in context.Products on categories equals product.Category into ppp
            //                      from p in ppp.DefaultIfEmpty()
            //                      where p.ProductName.Contains("l")
            //                      select new { categories/*, Products = ppp.DefaultIfEmpty().Where(p => p.ProductName.Contains("l"))*/ };// new {categories, Products = categories.Products.Where(p => p.ProductName.Contains("l"))});}
            //            var resutl = res.ToList();
            //
            //            foreach (var list1 in resutl)
            //            {
            //                Console.WriteLine(list1.categories.Products.Count);
            //            }
        }