开发者

Noob question: How to make a criteria on the count of an association?

开发者 https://www.devze.com 2022-12-26 17:39 出处:网络
I\'m starting with NHibernate. I have a type called Person which has a collection of Add开发者_StackOverflowress.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消