jnapier wrote:
I dont think you can do it with ICriteria but there is a simple way with HQL.
Code:
//create a filter on the collection
IQuery listQuery = this.Session.CreateFilter(documents, "Select count(*)");
//return the count
return (int)listQuery.UniqueResult();
I like to check if the collection has already been loaded yet though. That way, if the collection is already loaded, I dont have to hit the database.
Code:
if(documents is PersistentCollection){
//get the list as a proxy
PersistentCollection listProxy = documents as PersistentCollection;
//if the list hasnt been initialized, query the collection to determine the count
//this will be faster than bringing the whole collection down
if(listProxy.IsDirectlyAccessible == false){
//create a count filter for the collection
IQuery listQuery = this.Session.CreateFilter(documents, "Select count(*)");
//return the count
return (int)listQuery.UniqueResult();
}
//if we made it this far then use the regular ilist count operation
return documents.Count;
}
In regards to the code above, the first param of session.Filter is a collection. What I can't figure out is which collection I should be passing to that?
I basically have an application that searches for data and then displays it. However, if based on the search criteria, more than 50 items are going to be returned, the user has to narrow their search. The problem is that if the user enters criteria that matches 250,000 NHibernate loads all of them in order for me to get a count.
Thanks