sirrocco
sirrocco

Reputation: 8055

Multiple OR statements in EF4 query

I'm trying to create a query that would do something like :

column like 'a' or column like 'B' or column like 'C' //unknown number of OR

How can I do this in EF4 , in nHibernate I would have a disjunction.

Thanks.

Upvotes: 1

Views: 309

Answers (3)

Anton Kalcik
Anton Kalcik

Reputation: 2206

There are many approaches you can do this. But you should know the advantages and disadvantages for each one. See how performs LINQ to SQL, PredicateBuilder from LINQKit and Stored Procedures to accomplish this task!

http://kalcik.net/2014/01/05/joining-data-in-memory-with-data-in-database-table/

Upvotes: 0

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364269

If you want full match (your example show only full match) you can do this:

string items = new string[] { "a", "b", "c" };
var query = from x in context.Entities
            where items.Contains(x.Column)
            select x;

It works only in EF 4+ and it is translated to column IN ('a', 'b', c'). For more advanced generic solution you will really need dynamically build expression tree as mentioned by @Wouter de Kort or use ESQL where you write query as a string (and you can combine as many strings as you need). If you expect wildcard searching with complex search patterns you will have to use ESQL or at least create model defined function reused in dynamically created expression tree.

Upvotes: 0

Wouter de Kort
Wouter de Kort

Reputation: 39898

You would need to build an Expression Tree dynamically. Take a look at the PredicateBuilder.

The PredicateBuilder can help you to dynamically build expression trees.

Upvotes: 3

Related Questions