gpt4 book ai didi

asp.net-mvc - NHibernate QueryOver 与WhereRestriction 为 OR

转载 作者:行者123 更新时间:2023-12-02 21:25:39 25 4
gpt4 key购买 nike

我有这个查询,但我似乎找不到如何将WhereRestrictionOn设置为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();

非常感谢您的帮助!

解决方案:

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();

最佳答案

顶级 .Where() 系列(包括 WhereRestrictionOn) 始终使用 AND 连接。所以我们必须明确使用类似的东西:

  • 限制.Or(限制1, 限制1)
  • Restrictions.Disjunction().Add(restriction1).Add(restriction2).Add(...

所以,这可能是我们的情况:

.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))
)

正如此处讨论的:16.2. Simple Expressions ,对于简单的东西,我们甚至可以使用||(引用小例子):

.Where(p => p.Name == "test name" && (p.Age > 21 || p.HasCar))

关于asp.net-mvc - NHibernate QueryOver 与WhereRestriction 为 OR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24608118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com