3

I couldn't figure out how to create IsLike query with multiple conditions.

criteria =  criteria.Add(Restrictions.Like("IpAdress", "%" + request.Keyword + "%") ||
            Restrictions.Like("MacAdress", "%" + request.Keyword + "%") ||
            Restrictions.Like("al.SerialNumber", "%" + request.Keyword + "%"));

How to translate query above to IQueryOver format?

Thanks!

Ievgen Martynov
  • 7,870
  • 8
  • 36
  • 52

1 Answers1

8

You haven't posted what your entities look like, but you could write something along these lines:

query.Where(Restrictions.Disjunction()
    .Add(Restrictions.On<Type>(x => x.IpAddress).IsLike(request.Keyword))
    .Add(Restrictions.On<Type>(x => x.MacAdress).IsLike(request.Keyword))
    .Add(Restrictions.On<Type2>(x => x.SerialNumber).IsLike(request.Keyword)));

or you could use || operator instead of disjunction:

query.Where(
    Restrictions.On<Type>(x => x.IpAddress).IsLike(request.Keyword) ||
    Restrictions.On<Type>(x => x.MacAdress).IsLike(request.Keyword) ||
    Restrictions.On<Type2>(x => x.SerialNumber).IsLike(request.Keyword));

Here are some similar SO question for more info:
queryover and (x like 'a' or y like 'a')
QueryOver Or with Subquery

Community
  • 1
  • 1
Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47