[英]NHibernate QueryOver with WhereRestriction as OR
I have this query but I can't seem to find how I set my WhereRestrictionOn as an OR. 我有这个查询,但是我似乎找不到如何将WhereRestrictionOn设置为OR的方法。 Now they function as AND but I want one OR the other. 现在它们起AND的作用,但我要一个OR的另一个。
var privateInfo = Session.QueryOver<ConContact>()
.JoinAlias(c => c.PrivateInfos, () => pi)
.WhereRestrictionOn(c => c.FirstName).IsLike(_selectedFirstLetter + "%")
.WhereRestrictionOn(c => c.LastName).IsLike(_selectedFirstLetter + "%") // todo: change to firstname OR lastname
.Where(c => c.Status == ContactStatus.Approved)
.Select(
Projections.Property("pi.Id").WithAlias(() => sri.Id),
Projections.Property("FirstName").WithAlias(() => sri.Name), //todo: get fullname here => Add concontact object in privateinfo
Projections.Property("pi.Address").WithAlias(() => sri.Address),
Projections.Constant("Contact").WithAlias(() => sri.Type)
)
.TransformUsing(Transformers.AliasToBean<SearchResultInfo>())
.List<SearchResultInfo>()
.ToList();
Any help is much appreciated thx! 任何帮助深表感谢!
SOLUTION: 解:
var privateInfo = Session.QueryOver<ConContact>()
.JoinAlias(c => c.PrivateInfos, () => pi)
.Where(
Restrictions.Disjunction()
.Add(Restrictions.Like("FirstName", _selectedFirstLetter + "%"))
.Add(Restrictions.Like("LastName", _selectedFirstLetter + "%"))
)
.Where(c => c.Status == ContactStatus.Approved)
.Select(
Projections.Property("pi.Id").WithAlias(() => sri.Id),
Projections.Property("FirstName").WithAlias(() => sri.Name), //todo: get fullname here => Add concontact object in privateinfo
Projections.Property("pi.Address").WithAlias(() => sri.Address),
Projections.Constant(NewObjectType.Contact).WithAlias(() => sri.Type)
)
.TransformUsing(Transformers.AliasToBean<SearchResultInfo>())
.List<SearchResultInfo>()
.ToList();
The top level .Where()
family (including WhereRestrictionOn
) is always joined with AND. 顶级.Where()
系列(包括WhereRestrictionOn
)始终与AND结合在一起。 So we have to explicitly use something like: 因此,我们必须显式使用类似:
Restrictions.Or(restriction1, restriction1)
Restrictions.Disjunction().Add(restriction1).Add(restriction2).Add(...
So, this could be our case: 因此,这可能是我们的情况:
.Where(
Restrictions.Disjunction()
.Add(Restrictions.On<ConContact>(c => c.FirstName)
.IsLike(_selectedFirstLetter, MatchMode.Start))
.Add(Restrictions.On<ConContact>(c => c.LastName)
.IsLike(_selectedFirstLetter, MatchMode.Start))
// more OR ...
//.Add(Restrictions.On<ConContact>(c => c.MiddleName)
// .IsLike(_selectedFirstLetter, MatchMode.Start))
)
As discussed here: 16.2. 如此处所述: 16.2。 Simple Expressions , for simple stuff we can even use ||
简单表达式 ,对于简单的东西,我们甚至可以use ||
(cited small example) : (举个小例子) :
.Where(p => p.Name == "test name" && (p.Age > 21 || p.HasCar))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.