views:

520

answers:

1

Hi,

I'm trying to do a simple "Select Count(*) from PRODUCTS where date > xxx" with Castle on NHibernate.

If I was directly using NHibernate, I could reuse this question answers but unfortunately I see no easy way to access the Current NHibernate session from Castle Records.

I obviously don't want to retrieve all my objects and do a Count on the C# side ;). I only need to know how many objects there are.

Any ideas?

+5  A: 

There are many ways to do this, the simplest is:

ActiveRecordMediator<Product>.Count(Expression.Gt("date", DateTime.Now))

or your could use CountQuery, HQL, etc, there are many alternatives.

UPDATE: for NHibernate 2.0+ it's Restrictions instead of Expression.

I'll copy common questions like these to the ActiveRecord wiki.

Mauricio Scheffer
Count is marked as internal so I can't use it in my business layer (I don't really wish to reimplement it on the DAL side). The second one works perfectly, thanks!
Luk
You're right, although it is kind of weird that it's internal... I'll edit the answer, thanks.
Mauricio Scheffer
Which Expression class it this? I haven't found it.
ripper234
@ripper234: Expression has been renamed to Restrictions in NHibernate 2.0
Mauricio Scheffer