Ejemplo n.º 1
0
        public void DetachQueryAsSubqueryTest()
        {
            using var session = NHibernateHelper.OpenSession();

            var avgWeight = DetachedCriteria
                            .For <Cat>()
                            .SetProjection(Projections.Avg("Weight"));
            var actual = session
                         .CreateCriteria <Cat>()
                         .Add(Subqueries.PropertyGt("Weight", avgWeight))
                         .List <Cat>();

            var avgWeightExpected = _cats.Average(c => c.Weight);
            var expected          = _cats.Where(c => c.Weight > avgWeightExpected);

            AssertTwoListContainSameItems(actual.Select(c => c.Id), expected.Select(c => c.Id));

            var weights = DetachedCriteria
                          .For <Cat>()
                          .SetProjection(Projections.Property("Weight"));

            actual = session
                     .CreateCriteria <Cat>()
                     .Add(Subqueries.PropertyGeAll("Weight", weights))
                     .List <Cat>();
            var expectedCat = _cats.OrderByDescending(c => c.Weight).First();

            actual.Count.ShouldBe(1);
            actual.First().Id.ShouldBe(expectedCat.Id);
        }
Ejemplo n.º 2
0
        public void Property()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person))
                .Add(Subqueries.PropertyEq("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyGe("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyGt("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyIn("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyLe("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLt("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyNe("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyNotIn("Name", DetachedCriteriaName));

            var actual =
                CreateTestQueryOver <Person>()
                .WithSubquery.WhereProperty(p => p.Name).Eq(DetachedQueryOverName)
                .WithSubquery.WhereProperty(p => p.Age).Ge(DetachedQueryOverAge)
                .WithSubquery.WhereProperty(p => p.Age).Gt(DetachedQueryOverAge)
                .WithSubquery.WhereProperty(p => p.Name).In(DetachedQueryOverName)
                .WithSubquery.WhereProperty(p => p.Age).Le(DetachedQueryOverAge)
                .WithSubquery.WhereProperty(p => p.Age).Lt(DetachedQueryOverAge)
                .WithSubquery.WhereProperty(p => p.Name).Ne(DetachedQueryOverName)
                .WithSubquery.WhereProperty(p => p.Name).NotIn(DetachedQueryOverName);

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        public void Test_PropertyGtUsingAliasAlternativeSyntax()
        {
            ICriteria expected = CreateSession()
                                 .CreateCriteria(typeof(Person), "personAlias")
                                 .Add(Subqueries.PropertyGt("personAlias.Age", DetachedCriteriaSubquery));

            Person    personAlias = null;
            ICriteria actual      = CreateSession()
                                    .CreateCriteria(typeof(Person), () => personAlias)
                                    .Add(LambdaSubquery.Where(() => personAlias.Age > DetachedCriteriaSubquery.As <int>()));

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 4
0
        public void Test_PropertyGtUsingAlias()
        {
            ICriteria expected = CreateSession()
                                 .CreateCriteria(typeof(Person), "personAlias")
                                 .Add(Subqueries.PropertyGt("personAlias.Age", DetachedCriteriaSubquery));

            Person    personAlias = null;
            ICriteria actual      = CreateSession()
                                    .CreateCriteria(typeof(Person), () => personAlias)
                                    .Add(LambdaSubquery.Property(() => personAlias.Age).Gt(DetachedCriteriaSubquery));

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public void TestAllBuilderOverloads()
        {
            ICriteria expected = CreateSession()
                                 .CreateCriteria(typeof(Person))
                                 .Add(Subqueries.PropertyIn("Name", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyNotIn("Name", DetachedCriteriaSubquery))

                                 .Add(Subqueries.PropertyEq("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyNe("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGt("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGe("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLt("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLe("Age", DetachedCriteriaSubquery))

                                 .Add(Subqueries.PropertyEqAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGtAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGeAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLtAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLeAll("Age", DetachedCriteriaSubquery))

                                 .Add(Subqueries.PropertyGtSome("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGeSome("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLtSome("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLeSome("Age", DetachedCriteriaSubquery));

            ICriteria actual = CreateSession()
                               .CreateCriteria(typeof(Person))
                               .Add(LambdaSubquery.Property <Person>(p => p.Name).In(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Name).NotIn(DetachedCriteriaSubquery))

                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Eq(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Ne(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Gt(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Ge(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Lt(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).Le(DetachedCriteriaSubquery))

                               .Add(LambdaSubquery.Property <Person>(p => p.Age).EqAll(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).GtAll(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).GeAll(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).LtAll(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).LeAll(DetachedCriteriaSubquery))

                               .Add(LambdaSubquery.Property <Person>(p => p.Age).GtSome(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).GeSome(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).LtSome(DetachedCriteriaSubquery))
                               .Add(LambdaSubquery.Property <Person>(p => p.Age).LeSome(DetachedCriteriaSubquery));

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 6
0
        public void TestAllExpressionCombinations()
        {
            ICriteria expected = CreateSession()
                                 .CreateCriteria(typeof(Person), "personAlias")
                                 .Add(Subqueries.PropertyEq("personAlias.Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyNe("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGt("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGe("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLt("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLe("Age", DetachedCriteriaSubquery))

                                 .Add(Subqueries.PropertyEqAll("personAlias.Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGtAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGeAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLtAll("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLeAll("Age", DetachedCriteriaSubquery))

                                 .Add(Subqueries.PropertyGtSome("personAlias.Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyGeSome("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLtSome("Age", DetachedCriteriaSubquery))
                                 .Add(Subqueries.PropertyLeSome("Age", DetachedCriteriaSubquery));

            Person    personAlias = null;
            ICriteria actual      = CreateSession()
                                    .CreateCriteria(typeof(Person), () => personAlias)
                                    .Add(LambdaSubquery.Where(() => personAlias.Age == DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.Where <Person>(p => p.Age != DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.Where <Person>(p => p.Age > DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.Where <Person>(p => p.Age >= DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.Where <Person>(p => p.Age < DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.Where <Person>(p => p.Age <= DetachedCriteriaSubquery.As <int>()))

                                    .Add(LambdaSubquery.WhereAll(() => personAlias.Age == DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereAll <Person>(p => p.Age > DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereAll <Person>(p => p.Age >= DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereAll <Person>(p => p.Age < DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereAll <Person>(p => p.Age <= DetachedCriteriaSubquery.As <int>()))

                                    .Add(LambdaSubquery.WhereSome(() => personAlias.Age > DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereSome <Person>(p => p.Age >= DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereSome <Person>(p => p.Age < DetachedCriteriaSubquery.As <int>()))
                                    .Add(LambdaSubquery.WhereSome <Person>(p => p.Age <= DetachedCriteriaSubquery.As <int>()));

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 7
0
        public void PropertyAsSyntax()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person))
                .Add(Subqueries.PropertyEq("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyNe("Name", DetachedCriteriaName))
                .Add(Subqueries.PropertyGe("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyGt("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLe("Age", DetachedCriteriaAge))
                .Add(Subqueries.PropertyLt("Age", DetachedCriteriaAge));

            var actual =
                CreateTestQueryOver <Person>()
                .WithSubquery.Where(p => p.Name == DetachedQueryOverName.As <string>())
                .WithSubquery.Where(p => p.Name != DetachedQueryOverName.As <string>())
                .WithSubquery.Where(p => p.Age >= DetachedQueryOverAge.As <int>())
                .WithSubquery.Where(p => p.Age > DetachedQueryOverAge.As <int>())
                .WithSubquery.Where(p => p.Age <= DetachedQueryOverAge.As <int>())
                .WithSubquery.Where(p => p.Age < DetachedQueryOverAge.As <int>());

            AssertCriteriaAreEqual(expected, actual);
        }
Ejemplo n.º 8
0
        public void SubqueriesExpressions()
        {
            DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
                                  .Add(Expression.Eq("Name", "Gavin King"));
            ICriterion c = Subqueries.Eq("Gavin King", dc);

            NHAssert.IsSerializable(c);
            c = Subqueries.EqAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Exists(dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Ge("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GeAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GeSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Gt("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GtAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GtSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.In("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Le("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LeAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LeSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Lt("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LtAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LtSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Ne("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.NotExists(dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.NotIn("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyEq("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyEqAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGe("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGeAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGeSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGt("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGtAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGtSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyIn("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLe("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLeAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLeSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLt("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLtAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLtSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyNe("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyNotIn("Name", dc);
            NHAssert.IsSerializable(c);
        }