NHibernate QueryOver with WhereRestriction as OR

眉间皱痕 提交于 2019-11-29 15:49:18

The top level .Where() family (including WhereRestrictionOn) is always joined with 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. Simple Expressions, for simple stuff we can even use || (cited small example):

.Where(p => p.Name == "test name" && (p.Age > 21 || p.HasCar))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!