Queryover and (x like 'a' or y like 'a')

Hi Is there an elegant way to combine like and or when I use the Query Query 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 the code is '% abc%' or a description of type '% abc%'

+49
nhibernate queryover
Dec 27 '10 at 20:20
source share
3 answers
 query.Where(Restrictions.On<Type>(x => x.Code).IsLike(codePart) || Restrictions.On<Type>(x => x.Description).IsLike(codePart)) 
+55
Dec 27 2018-10-12-27
source share

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

 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 that helps if you want to conditionally add predicates.

+59
Jan 27 '11 at 9:11
source share

Another version of this that you may like depending on your taste 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))); 
+9
Dec 06 '11 at 3:18
source share



All Articles