8

I want to create a query which has more than 3-4 Expression.Or ? But Expression.Or just let me to add two Expressions inside it.

if (!string.IsNullOrEmpty(keyword))
                query
                    .Add(Expression.Or(
                             Expression.Like("Name", keyword, MatchMode.Anywhere),
                             Expression.Like("LastName", keyword, MatchMode.Anywhere)))
                    .Add(Expression.Or(
                             Expression.Like("Email1", keyword, MatchMode.Anywhere),
                             Expression.Like("Email2", keyword, MatchMode.Anywhere)));

The code above generates "Name like %this% or LastName like %this% AND Email1 like %this% and Email2 like %this.

Thanks in advance.

Barbaros Alp
  • 6,405
  • 8
  • 47
  • 61
  • The following is something I found a while back for Entity Framework, the same code works with nHibernate : http://stackoverflow.com/questions/1554663/composing-linq-to-entity-query-from-multiple-parameters/1554759#1554759 – Alexandre Brisebois Jul 18 '11 at 21:02
  • @Alexandre: your solution is for a LINQ provider. This question is about the NHibernate Criteria API. – Mauricio Scheffer Jul 18 '11 at 22:18

2 Answers2

8

Use Disjunction instead of Or.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
2

You can also use || instead of Or( ) or Disjunction( ).

Caro
  • 91
  • 1
  • 4