Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 = 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();
share|improve this question

1 Answer 1

up vote 0 down vote accepted

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))
share|improve this answer
    
Thank you! This worked for me, solution posted in topic. –  Nanou Ponette Jul 7 '14 at 10:26
    
Great to see that! Enjoy NHiberante ;) ... Maybe, try to re-think the use of the MatchMode as I suggested –  Radim Köhler Jul 7 '14 at 10:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.