How to set more than 2 Expression in Expression.Or
Asked Answered
E

2

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.

Eleanoraeleanore answered 12/1, 2009 at 0:51 Comment(2)
The following is something I found a while back for Entity Framework, the same code works with nHibernate : #1555163Chivy
@Alexandre: your solution is for a LINQ provider. This question is about the NHibernate Criteria API.Witch
W
8

Use Disjunction instead of Or.

Witch answered 12/1, 2009 at 1:14 Comment(5)
Thank you very much, I have another question. I want to query another table while querying this one. Forexample i want to get Customers whose id = Group.CustomerIdEleanoraeleanore
Please create another question for that, it's unrelated to this one.Witch
I really don't understand the point of using Disjunction when you have the Linq api and ExpressionsChivy
@Alexandre: take a good look at the date of the question and answer. Also, the question is tagged with 'criteria'Witch
@Alexandre: if there was a LINQ provider for NHibernate back then, it was the contrib provider which was quite broken. Criteria API was much more widely used. The built-in LINQ provider is a relatively recent addition.Witch
T
2

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

Thyratron answered 31/7, 2009 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.