NHibernate MultiCriteria
Today I ran across an interesting problem – I needed to query to get counts after a process runs. Obviously I can make two database calls, but I instead wanted to take advantage of NHibernate’s ‘MultiCriteria’ capability. This will make one call to the database with the 2 queries vs. 2 database calls. My example:
IMultiCriteria crit = NHibernateSession.Current.CreateMultiCriteria() .Add(CreateCriteria<CompanyDeposit>() .Add(Restrictions.Between("ProcessedDate", lo, hi)) .SetProjection(Projections.RowCount())) .Add(CreateCriteria<EmployeeDeposit>() .Add(Restrictions.Between("ProcessedDate", lo, hi)) .SetProjection(Projections.RowCount())); IList results = crit.List(); var companies = (IList)results[0]; var employees = (IList) results[1]; var companyCount = (int)companies[0]; var employeeCount = (int) employees[0];
One queries CompanyDeposits and gets the rowcount, the other queries EmployeeDeposits and gets it’s rowcount.
Slick…
For reference, check out Ayende’s post here as well
(and thanks to Stefan for asking me about it, which made me want to go try it out… lol)