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); }
public IAggregateRoot LoadById(Guid id) { DetachedCriteria boundaryCriteria = DetachedCriteria.For <Event>() .SetProjection(Projections.Id()) .Add(Expression.Eq("IsSnapshot", true)) .Add(Expression.Eq("EntityId", id)) .AddOrder(new Order("SequenceNumber", false)) .SetFirstResult(0) .SetMaxResults(1); ICriteria criteria = _readSession.CreateCriteria(typeof(Event)) .Add(Expression.Eq("EntityId", id)) .Add(Subqueries.PropertyGe("SequenceNumber", boundaryCriteria)) .AddOrder(new Order("SequenceNumber", true)); IList <Event> events = criteria.List <Event>(); if (events.Count < 1) { throw new ArgumentException(string.Format("Entity with id {0} not found.", id)); } IAggregateRoot lastSnapshot = (IAggregateRoot)events[0].Data; lastSnapshot.Id = id; lastSnapshot.LoadFromEventStream(events.Skip(1).Select(x => x.Data)); lastSnapshot.Version = events.Last().Version; _trackedObjects.Add(lastSnapshot); return(lastSnapshot); }
public IList <User> GeAvgAgeUsers() { var crit = session.CreateCriteria <User>(); var detachCriteria = DetachedCriteria.For <User>() //подзапрос .SetProjection(Projections.Avg("Age")); crit.Add(Subqueries.PropertyGe("Age", detachCriteria)); return(crit.List <User>()); }
public IList <User> GeAvgAgeUsers() { var crit = session.CreateCriteria <User>(); var detachCriteria = DetachedCriteria.For <User>() .SetProjection(Projections.Avg("Age")); crit.Add(Subqueries.PropertyGe("Age", detachCriteria)); return(crit.List <User>()); //"select * from User where Age >= (select avg(Age) from User)" }
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); }
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); }
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); }
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); }