|
From: <ric...@us...> - 2009-11-30 17:10:51
|
Revision: 4877
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4877&view=rev
Author: ricbrown
Date: 2009-11-30 17:10:42 +0000 (Mon, 30 Nov 2009)
Log Message:
-----------
Added integration example demonstrating multiple projections and sub-queries inline.
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs
trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs
trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Mappings.hbm.xml
Modified: trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2009-11-30 13:28:06 UTC (rev 4876)
+++ trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2009-11-30 17:10:42 UTC (rev 4877)
@@ -264,7 +264,7 @@
return this;
}
- QueryOverProjectionBuilder<QueryOver<TRoot,TSubType>, TRoot, TSubType> SelectList
+ public QueryOverProjectionBuilder<QueryOver<TRoot,TSubType>, TRoot, TSubType> SelectList
{
get { return new QueryOverProjectionBuilder<QueryOver<TRoot,TSubType>, TRoot, TSubType>(this, this); }
}
Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2009-11-30 13:28:06 UTC (rev 4876)
+++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2009-11-30 17:10:42 UTC (rev 4877)
@@ -205,6 +205,66 @@
}
}
+ [Test]
+ public void SubQuery()
+ {
+ using (ISession s = OpenSession())
+ using (ITransaction t = s.BeginTransaction())
+ {
+ s.Save(new Person() { Name = "Name 1", Age = 1 }
+ .AddChild(new Child() { Nickname = "Name 1.1", Age = 1}));
+
+ s.Save(new Person() { Name = "Name 2", Age = 2 }
+ .AddChild(new Child() { Nickname = "Name 2.1", Age = 2})
+ .AddChild(new Child() { Nickname = "Name 2.2", Age = 2}));
+
+ s.Save(new Person() { Name = "Name 3", Age = 3 }
+ .AddChild(new Child() { Nickname = "Name 3.1", Age = 3}));
+
+ t.Commit();
+ }
+
+ using (ISession s = OpenSession())
+ {
+ Person personAlias = null;
+ object childCountAlias = null;
+
+ QueryOver<Child> averageChildAge =
+ QueryOver.Of<Child>()
+ .SelectList
+ .SelectAvg(c => c.Age)
+ .EndSelect;
+
+ QueryOver<Child> childCountQuery =
+ QueryOver.Of<Child>()
+ .Where(c => c.Parent.Id == personAlias.Id)
+ .Select(Projections.RowCount());
+
+ var nameAndChildCount =
+ s.QueryOver<Person>(() => personAlias)
+ .WithSubquery.Where(p => p.Age <= averageChildAge.As<int>())
+ .SelectList
+ .Select(p => p.Name)
+ .SelectSubQuery(childCountQuery).WithAlias(() => childCountAlias)
+ .EndSelect
+ .OrderBy(() => childCountAlias).Desc
+ .List<object[]>()
+ .Select(props => new {
+ Name = (string)props[0],
+ ChildCount = (int)props[1],
+ })
+ .ToList();
+
+ Assert.That(nameAndChildCount.Count, Is.EqualTo(2));
+
+ Assert.That(nameAndChildCount[0].Name, Is.EqualTo("Name 2"));
+ Assert.That(nameAndChildCount[0].ChildCount, Is.EqualTo(2));
+
+ Assert.That(nameAndChildCount[1].Name, Is.EqualTo("Name 1"));
+ Assert.That(nameAndChildCount[1].ChildCount, Is.EqualTo(1));
+ }
+ }
+
}
}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Mappings.hbm.xml
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Mappings.hbm.xml 2009-11-30 13:28:06 UTC (rev 4876)
+++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/Mappings.hbm.xml 2009-11-30 17:10:42 UTC (rev 4877)
@@ -21,6 +21,7 @@
<generator class="native"/>
</id>
<property name="Nickname" />
+ <property name="Age" />
<many-to-one name="Parent" class="Person" />
</class>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|