嗨:我试过看其他帖子了。如果这是一个重复的帖子,我会提前道歉。
sql查询是这样的:
select * from ApiData where BrandID = 'SOUPBRAND' and Culture = 'de-de'
or
( BrandID = 'SOUPBRAND' and Culture = 'pt-br' and EntityType = 'contactus')
or
(BrandID = 'examplefoods')
nhibernate查询会是什么样的?
这是我到目前为止所做的:
var mainQuery = _session.QueryOver<ApiData>();
foreach (var brandPermission in permissions)
{
var query = _session.QueryOver<ApiData>();
if (!string.IsNullOrEmpty(brandPermission.Culture) && !string.IsNullOrEmpty(brandPermission.Publication))
{
query = query.Where(a => (a.Culture == brandPermission.Culture)
&& (a.Publication == brandPermission.Publication)); //FetchEntries(apiUser.DefaultCulture, apiUser.Publication);
}
else if (!string.IsNullOrEmpty(brandPermission.Culture))
{
query = query.Where(a => (a.Culture == brandPermission.Culture)); //FetchEntries(apiUser.DefaultCulture);
}
query = query.Where(a => a.BrandId == brandPermission.BrandId);
//Here I would like to add
// mainQuery.AddORRestriction(query);
}
只是不知道如何实现它。看到一个链接:http://www.methodicmadness.com/2012/04/extending-queryover-with-or.html但没有帮助。
谢谢!
答案 0 :(得分:5)
感谢:https://www.google.com/url?q=http://ayende.com/blog/4023/nhibernate-queries-examples&usd=2&usg=AFQjCNG43MDXKhjZ9LrCVSldeZuruBSdQQ 并:https://www.google.com/url?q=https://stackoverflow.com/questions/14478430/c-sharp-nhibernate-multiple-where-conditions-in-session-queryover&usd=2&usg=AFQjCNHRKbQz3EKGqOOh0gSV0LW2taSX-w
我得到了以下解决方案:
var mainQuery = _session.QueryOver<ApiData>();
var holdOr = Restrictions.Disjunction();
foreach (var brandPermission in permissions)
{
var query = _session.QueryOver<ApiData>();
var holdAnd = Restrictions.Conjunction();
if (!string.IsNullOrEmpty(brandPermission.Culture) && !string.IsNullOrEmpty(brandPermission.Publication))
{
holdAnd.Add(Restrictions.Eq("Culture", brandPermission.Culture));
holdAnd.Add(Restrictions.Eq("Publication", brandPermission.Publication));
}
else if (!string.IsNullOrEmpty(brandPermission.Culture))
{
holdAnd.Add(Restrictions.Eq("Culture", brandPermission.Culture));
}
holdAnd.Add(Restrictions.Eq("BrandId", brandPermission.BrandId));
holdOr.Add(holdAnd);
}
mainQuery.Where(holdOr);