-1

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.

i3havi
  • 35
  • 9
  • finally I found solution: – i3havi Apr 18 '14 at 06:48
  • 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(); – i3havi Apr 18 '14 at 06:51
  • Not sure why the down vote... it is interesting question, and your own answer is really nice. Please, take it from a comment and append it as real (later accept it as well) answer... it could be of some use later. thanks – Radim Köhler Apr 18 '14 at 07:18

1 Answers1

0

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.

i3havi
  • 35
  • 9