本文介绍了NHibernate的或标准查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下映射类
Trade { ID, AccountFrom, AccountTo }
Account {ID, Company}
Company {ID}
现在我不能想出一个办法选择所有行业,其中
Now I cannot figure out a way select all trades where
AccountFrom.Company.ID = X OR AccountTo.Company.ID = X
我可以获取和使用以下工作:
I can get AND to work using the following:
criteria.CreateCriteria("AccountFrom").CreateCriteria("Company").Add(Restrictions.Eq("ID", X);
criteria.CreateCriteria("AccountTo").CreateCriteria("Company").Add(Restrictions.Eq("ID", X);
但我怎么能转化成该或者更确切地说,是一个与我已经使用析取以前,但我似乎无法知道如何添加单独的标准,只是限制。
But how can I transform this into an OR rather an an AND. I have used Disjunction previously, but I cannot seem to know how to add separate criteria, just restrictions.
推荐答案
尝试:
return session.CreateCriteria<Trade>()
.CreateAlias("AccountFrom", "af")
.CreateAlias("AccountTo", "at")
.Add(Restrictions.Or(
Restrictions.Eq("af.Company.CompanyId", companyId),
Restrictions.Eq("at.Company.CompanyId", companyId)))
.List<Trade>();
我不认为你需要的别名公司。
I don't think you will need to alias Company.
这篇关于NHibernate的或标准查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!