queryover and (x like 'a' or y like 'a')
Asked Answered
I

3

51

Hi Is there any elegant way of combining 'like' and 'or' when i'm using queryover API? for 'like' there is something like:

 query.WhereRestrictionOn(x=>x.Code).IsLike(codePart)

for 'or' i can do something like:

query.Where( x=>x.Code == codePart || x.Description== codePart)

but how can I create a query like this:

select * from n where code like '%abc%' or description like '%abc%'

Impart answered 27/12, 2010 at 20:20 Comment(0)
A
57
query.Where(Restrictions.On<Type>(x => x.Code).IsLike(codePart) ||
            Restrictions.On<Type>(x => x.Description).IsLike(codePart))
Agitation answered 27/12, 2010 at 21:42 Comment(0)
A
60

You could use the NHibernate Disjunction class to do this in a more elegant (IMHO) fashion:

var disjunction= new Disjunction();

disjunction.Add(Restrictions.On<Type>(e => e.Code).IsLike(codePart));
disjunction.Add(Restrictions.On<Type>(e => e.Description).IsLike(codePart));
//(and so on)

and then:

query.Where(disjunction)

Each "OR" is a separate instruction, which helps if you want to add the predicates conditionally.

Aguilar answered 27/1, 2011 at 9:11 Comment(0)
A
57
query.Where(Restrictions.On<Type>(x => x.Code).IsLike(codePart) ||
            Restrictions.On<Type>(x => x.Description).IsLike(codePart))
Agitation answered 27/12, 2010 at 21:42 Comment(0)
C
9

Another version of this, which depending on taste, you may like, is as follows:

query.Where(Restrictions.Disjunction()
         .Add(Restrictions.On<Type>(e => e.Code).IsLike(codePart))
         .Add(Restrictions.On<Type>(e => e.Description).IsLike(codePart)));
Colwen answered 6/12, 2011 at 3:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.