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; var employees = (IList) results; var companyCount = (int)companies; var employeeCount = (int) employees;
One queries CompanyDeposits and gets the rowcount, the other queries EmployeeDeposits and gets it’s rowcount.
(and thanks to Stefan for asking me about it, which made me want to go try it out… lol)