Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,026 | Comments: 44,842

filter by tags archive

Slicing & Dicing Queries with NHibernate

time to read 1 min | 89 words

This was how I started the day, interesting problem, and I like the solution:

DetachedCriteria getMaxScanDateForScan = DetachedCriteria.For<ScanResult>()
DetachedCriteria hasResults = DetachedCriteria.For<ScanResult>()

	.CreateCriteria("Results", "result", JoinType.LeftOuterJoin)
		.Add(Projections.Property("scan.Id"), "Id")
		.Add(Projections.Property("scan.Uri"), "Uri")
		.Add(Projections.Property("scan.DaysNotice"), "DaysNotice")
		.Add(Projections.Property("scan.EnableScan"), "EnableScan")
		.Add(Projections.Property("result.ScanDate"), "LastScan")
		.Add(Projections.Property("result.Status"), "Status")
		.Add(Projections.Property("result.Message"), "Message")
		.Add(Subqueries.PropertyEq("result.ScanDate", getMaxScanDateForScan))

IList list = criteria
		.SetResultTransformer(new AliasToBeanResultTransformer(typeof (ScanURIReport)))



Very intuitive....lol :)

What does the SetProjection call do as far as the TSQL generated?

Ayende Rahien

Think about SetProjection as setting what will be in the SELECT clause.


intersting.. (still new to nhibernate) basically you're setting up the subqueries in seperate statements.?

Markus Zywitza

I wish that such examples with explanation were part of the NHibernate Reference. Its section on Criteria Queries is quite short and covers only the basics :-(


Craig Neuwirt

Where is the scan alias set and where is criteria created

Ayende Rahien


ICriteria criteria = DetachedCriteria.For("scan");

Comment preview

Comments have been closed on this topic.


No future posts left, oh my!


  1. Technical observations from my wife (3):
    13 Nov 2015 - Production issues
  2. Production postmortem (13):
    13 Nov 2015 - The case of the “it is slow on that machine (only)”
  3. Speaking (5):
    09 Nov 2015 - Community talk in Kiev, Ukraine–What does it take to be a good developer
  4. Find the bug (5):
    11 Sep 2015 - The concurrent memory buster
  5. Buffer allocation strategies (3):
    09 Sep 2015 - Bad usage patterns
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats