NHibernate 2.0 beta1 was release a couple of days ago, so I thought it was time to go through some more new stuff that has been added since version 1.2. I found a new useful helper class named CritieraTransformer which can be used to clone ICritiera queries. This is very useful if you want to perform multiple queries with small variations (in the where clause for example). It also has a useful function that adds a row count projection to a query.
var customersCritiera = session.CreateCritiera(typeof(Customer)); var totalCustomersCritiera = CriteriaTransformer.TransformToRowCount(customersCritiera); var inGroupCriteria = CriteriaTransformer.Clone(customersCritiera) .Add(Expression.Eq("Group", group)); var goldenInGroupCritiera = CriteriaTransformer.Clone(inGroupCriteria) Add(Expression.Eq("IsGold", true); IList results = session.CreateMultiCriteria() .Add(inGroupCriteria) .Add(goldenInGroupCritiera) .Add(totalCustomersCritiera) .List(); IList inGroupCustomers = (IList)results; IList customers = (IList)results; IList counts = (IList)results; int count = (int)counts;
The multi criteria feature allows you peform multiple database queries in a single database call, the downside is that the IMultiCritiera interface does not support generic lists.