I'm building an ad-hoc query to send to SQL doing the following:
我正在构建一个即席查询,以便发送到SQL执行以下操作:
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.
问题是WHERE子句与AND在一起。我需要OR。
Any idea how to get an OR
?
知道如何获得OR吗?
2 个解决方案
#2
You need to construct an Expression to represent the composite OR predicate, then pass that to Where:
您需要构造一个Expression来表示复合OR谓词,然后将其传递给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 :)
编辑:这可能是PredicateBuilder所做的,只是很麻烦:)
#1
How about I answer my own question: PredicateBuilder
我怎么回答我自己的问题:PredicateBuilder
#2
You need to construct an Expression to represent the composite OR predicate, then pass that to Where:
您需要构造一个Expression来表示复合OR谓词,然后将其传递给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 :)
编辑:这可能是PredicateBuilder所做的,只是很麻烦:)