Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top