开发者

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜