I want to count set of set of model in NHibernate using Criteria Query.

Account Model have Contacts(set) and Contact Model have Addresses(set).

I want to count addresses by giving input Account object.

I have implemented temporary by simple foreach loop.

If anyone know then please help me.

Thanks in advanced.

有帮助吗?

解决方案

Thanks Radim Köhler,

I found my solution by :

var count = (Int32)Session.CreateCriteria(typeof(Account)) .Add(Restrictions.Eq("Id", account.Id)) .CreateCriteria("Contacts", "Contacts", JoinType.InnerJoin, Restrictions.IsNotEmpty("Addresses")) .SetProjection(Projections.Count("Id")).UniqueResult(); .

Then I have used following criteria query:

var count = (Int32)Session.CreateCriteria(typeof(Address))
                    .CreateCriteria("Contact", "Contact",JoinType.InnerJoin)
                    .Add(Restrictions.Eq("Account.Id",accountId))
                    .SetProjection(Projections.Count("Id")).UniqueResult();

This give me actual result that I want by optimal query.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top