Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
163 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...