public void Correlated()
        {
            OrderItemCollection collection = new OrderItemCollection();
            collection.es.Connection.ConnectionString =
                UnitTestBase.GetFktString(collection.es.Connection);

            OrderItemQuery oiq = new OrderItemQuery("oi");
            ProductQuery pq = new ProductQuery("p");

            // oiq.ProductID in the inner Select is pulled from
            // the outer Select, making a correlated SubQuery.
            oiq.Select(
                oiq.OrderID,
                (oiq.Quantity * oiq.UnitPrice).Sum().As("Total")
            );
            oiq.Where(oiq.ProductID
                .In(
                    pq.Select(pq.ProductID)
                    .Where(oiq.ProductID == pq.ProductID)
                )
            );
            oiq.GroupBy(oiq.OrderID);

            Assert.IsTrue(collection.Load(oiq));
            Assert.AreEqual(5, collection.Count);
        }
        public void OneWhereSubtract()
        {
            OrderItemCollection collection = new OrderItemCollection();
            collection.es.Connection.Name = "ForeignKeyTest";

            OrderItemQuery oiq = new OrderItemQuery("oiq");

            oiq.Select(oiq.OrderID, oiq.ProductID,
                (oiq.UnitPrice - oiq.Discount).As("SomeDecimal"));
            oiq.Where(oiq.UnitPrice - oiq.Discount < .2);
            oiq.OrderBy(oiq.OrderID, esOrderByDirection.Ascending);
            oiq.OrderBy(oiq.ProductID, esOrderByDirection.Ascending);

            Assert.IsTrue(collection.Load(oiq));

            decimal someDec = Convert.ToDecimal(collection[0].GetColumn("SomeDecimal"));
            Assert.AreEqual(Convert.ToDecimal("0.10"), Math.Round(someDec, 2));
        }
        public void OneModLiteral()
        {
            OrderItemCollection collection = new OrderItemCollection();
            collection.es.Connection.Name = "ForeignKeyTest";


            switch (collection.es.Connection.ProviderSignature.DataProviderName)
            {
                case "EntitySpaces.MSAccessProvider":
                    Assert.Ignore("Not supported");
                    break;
                default:

                    OrderItemQuery oiq = new OrderItemQuery("oiq");

                    oiq.Select(oiq.OrderID, oiq.ProductID,
                        (oiq.Quantity % 2).As("SomeInteger"));
                    oiq.OrderBy(oiq.OrderID, esOrderByDirection.Ascending);
                    oiq.OrderBy(oiq.ProductID, esOrderByDirection.Ascending);

                    Assert.IsTrue(collection.Load(oiq));

                    decimal someInt = Convert.ToInt32(collection[0].GetColumn("SomeInteger"));
                    Assert.AreEqual(1, someInt);
                    break;
            }
        }
        public void HavingWithSimpleExpression()
        {
            OrderItemCollection coll = new OrderItemCollection();
            coll.es.Connection.Name = "ForeignKeyTest";

            OrderItemQuery q = new OrderItemQuery();
            //q.es2.Connection.Name = "ForeignKeyTest";

            q.Select(q.OrderID, q.Quantity.Sum().As("TotalQty"));
            q.Where(q.Discount.IsNull());
            q.GroupBy(q.OrderID);

            switch (coll.es.Connection.ProviderSignature.DataProviderName)
            {
                case "EntitySpaces.SqlServerCeProvider":
                case "EntitySpaces.SqlServerCe4Provider":
                case "EntitySpaces.VistaDBProvider":
                case "EntitySpaces.VistaDB4Provider":
                    q.Having(q.Quantity.Sum() > 100);
                    q.OrderBy("<TotalQty>", esOrderByDirection.Descending);
                    break;

                case "EntitySpaces.SQLiteProvider":
                    q.Having((q.Quantity * 1).Sum() > 100);
                    q.OrderBy(q.Quantity.Sum().Descending);
                    break;

                default:
                    q.Having(q.Quantity.Sum() > 100);
                    q.OrderBy(q.Quantity.Sum().Descending);
                    break;
            }

            Assert.IsTrue(coll.Load(q), "Load");
            Assert.AreEqual(3, coll.Count, "Count");

            int qty = Convert.ToInt32(coll[0].GetColumn("TotalQty"));
            Assert.AreEqual(240, qty, "GetColumn");
        }
        public void HavingWithComplexExpression()
        {
            OrderItemCollection coll = new OrderItemCollection();
            coll.es.Connection.Name = "ForeignKeyTest";

            OrderItemQuery q = new OrderItemQuery();
            q.Select(q.OrderID, (q.Quantity * q.UnitPrice).Sum().As("TotalPrice"));
            q.Where(q.Discount.IsNull());
            q.GroupBy(q.OrderID);
            q.Having((q.Quantity * q.UnitPrice).Sum() > 500);

            switch (coll.es.Connection.ProviderSignature.DataProviderName)
            {
                case "EntitySpaces.SqlServerCeProvider":
                case "EntitySpaces.SqlServerCe4Provider":
                case "EntitySpaces.VistaDBProvider":
                case "EntitySpaces.VistaDB4Provider":
                    q.OrderBy("<TotalPrice>", esOrderByDirection.Descending);
                    break;

                default:
                    q.OrderBy((q.Quantity * q.UnitPrice).Sum().Descending);
                    break;
            }

            Assert.IsTrue(coll.Load(q), "Load");
            Assert.AreEqual(2, coll.Count, "Count");

            decimal price = Convert.ToDecimal(coll[0].GetColumn("TotalPrice"));
            Assert.AreEqual(1940.0M, price, "GetColumn");
        }
        public void NestedZeroToMany()
        {
            // The main Employee query
            EmployeeQuery eq1 = new EmployeeQuery("e");
            eq1.Where(eq1.EmployeeID < 4);
            eq1.OrderBy(eq1.EmployeeID.Ascending);

            // The Order Collection
            OrderQuery oq1 = eq1.Prefetch<OrderQuery>(Employee.Prefetch_OrderCollectionByEmployeeID);
            EmployeeQuery eq2 = oq1.GetQuery<EmployeeQuery>();
            oq1.Where(eq2.EmployeeID < 4 && oq1.PlacedBy < 3);

            // Pre-test the Order query
            OrderCollection oColl = new OrderCollection();
            oColl.es.Connection.Name = "ForeignKeyTest";
            oColl.Load(oq1);
            Assert.AreEqual(5, oColl.Count, "Order pre-test");
            
            // The OrderItem Collection
            OrderItemQuery oiq1 = eq1.Prefetch<OrderItemQuery>(Employee.Prefetch_OrderCollectionByEmployeeID, Order.Prefetch_OrderItemCollectionByOrderID);
            EmployeeQuery eq3 = oiq1.GetQuery<EmployeeQuery>();
            OrderQuery oq2 = oiq1.GetQuery<OrderQuery>();
            oiq1.Where(eq3.EmployeeID < 4 && oq2.PlacedBy < 3 && oiq1.Quantity < 100);

            // Pre-test the OrderItem query
            OrderItemCollection oiColl = new OrderItemCollection();
            oiColl.es.Connection.Name = "ForeignKeyTest";
            oiColl.Load(oiq1);
            Assert.AreEqual(4, oiColl.Count, "OrderItem pre-test");

            // Will Prefetch the Order and OrderItems queries
            EmployeeCollection coll = new EmployeeCollection();
            coll.es.Connection.Name = "ForeignKeyTest";
            //coll.es.IsLazyLoadDisabled = true;
            coll.Load(eq1);

            foreach (Employee emp in coll)
            {
                emp.es.IsLazyLoadDisabled = true;

                switch (emp.EmployeeID.Value)
                {
                    case 1:
                        Assert.AreEqual(1, emp.EmployeeID.Value);
                        Assert.AreEqual(0, emp.OrderCollectionByEmployeeID.Count);
                        break;

                    case 2:
                        Assert.AreEqual(2, emp.EmployeeID.Value);
                        Assert.AreEqual(2, emp.OrderCollectionByEmployeeID.Count);

                        foreach (Order o in emp.OrderCollectionByEmployeeID)
                        {
                            Assert.Less(0, o.OrderItemCollectionByOrderID.Count);
                        }
                        break;

                    case 3:
                        Assert.AreEqual(3, emp.EmployeeID.Value);
                        Assert.AreEqual(3, emp.OrderCollectionByEmployeeID.Count);

                        foreach (Order o in emp.OrderCollectionByEmployeeID)
                        {
                            Assert.AreEqual(0, o.OrderItemCollectionByOrderID.Count);
                        }
                        break;

                    default:
                        Assert.Fail("Only employees 1, 2, and 3 should be loaded.");
                        break;
                }
            }
        }