views:

62

answers:

1

I'm starting with NHibernate.

I have a type called Person which has a collection of Address.

How do I fetch:

All people with at least 2 addresses

using ICriteria? Thanks in advance.

+1  A: 

For this you need to use subqueries.

Address alias = null;
ICriteria criteria = personsCriteria.CreateCriteria<Person>(x => x.Address, () => alias);
var addressCount = DetachedCriteria.For<Address>();
addressCount.SetProjection(Projections.RowCount());
addressCount.Add<Address>(x => x.User.Id == alias.Id);
criteria.Add(Subqueries.Eq(2, addressCount));

I'm using ICriteria lambda extensions. You can look at them here

Sly