在Nhibernate中计算模型的第二级集合 [英] Count second level set of model in nhibernate
问题描述
我想使用条件查询来计算NHibernate中的模型集.
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.
我已经通过简单的foreach循环实现了临时功能.
I have implemented temporary by simple foreach loop.
如果有人知道,请帮助我.
If anyone know then please help me.
非常感谢.
推荐答案
感谢RadimKöhler,
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();
.
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.
这篇关于在Nhibernate中计算模型的第二级集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!