views:

81

answers:

2

I'm building an ad-hoc query to send to SQL doing the following:

var data = from d in db.xxxx select d;
foreach (pattern in user_stuff)
   data = data.Where(d=>SqlMethods.Like(d.item,pattern)==true);

The problem is that the WHERE clauses get AND'ed together. I need OR's.

Any idea how to get an OR?

+3  A: 

How about I answer my own question: PredicateBuilder

Wavel
the || operator dint work for you??
Perpetualcoder
PerpetualCoder, you can't || together a dynamic list of predicates. That's why you need a predicate builder.
Avish
+2  A: 

You need to construct an Expression to represent the composite OR predicate, then pass that to Where:

var data = from d in db.xxxx select d;
Expression<Func<Data, bool>> predicate = null;
foreach (var pattern in user_stuff)
{
    Expression<Func<Data, bool>> newPredicate = d => SqlMethods.Like(d..item, pattern));
    if (predicate == null) predicate = newPredicate;
    else predicate = Expression.OrElse(predicate, newPredicate);
}
return data.Where(predicate);


EDIT: This is probably what PredicateBuilder does, only a lot messier :)

Avish