NHibernate QueryOver with WhereRestriction as OR

前端 未结 1 1553
渐次进展
渐次进展 2020-12-21 14:34

I have this query but I can\'t seem to find how I set my WhereRestrictionOn as an OR. Now they function as AND but I want one OR the other.

var privateInfo =         


        
相关标签:
1条回答
  • 2020-12-21 14:42

    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))
    
    0 讨论(0)
提交回复
热议问题