我正在构建一个即席查询,以便向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);
问题是WHERE
条款一起得到AND
。我需要OR
。
知道如何获得OR
?
答案 0 :(得分:3)
我如何回答我自己的问题: PredicateBuilder
答案 1 :(得分:2)
您需要构造一个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);
编辑:这可能是PredicateBuilder所做的,只是很麻烦:)