Grouping with ICriteria in NHibernate

June 29, 2009


Finally, i managed to find how to do grouping with NHibernate. Basically, what you need to know is that grouping and aggreagte functions are all made with IProjection.  A quick example would be:

return session.CreateCriteria(typeof(HumanTask))
.CreateCriteria(“Human”, “responsibleuser”)
.SetProjection(Projections.ProjectionList()
.Add(Projections.RowCount())
.Add(Property.ForName(“responsibleuser.FullName”))
.Add(Property.ForName(“responsibleuser.FullName”).Group())
);

What this does is: group tasks assigned to human, and returned the count and name for each group.  A nice way to understand how to use some features of NH is to read it’s code. I found this by looking at NHibernate.Test.Criteria.CriteriaQueryTest class. There is a nice method which creates a lot of criterias. that method is CloningProjectionsTest, have a look at it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: