Noob question: How to make a criteria on the count of an association?
I'm starting with NHibernate.
I have a type called Person
which has a collection of Add开发者_StackOverflowress
.
How do I fetch:
All people with at least 2 addresses
using ICriteria? Thanks in advance.
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
精彩评论