Linq To Sql 'Where Or' 运算符 [英] Linq To Sql 'Where Or' operator
本文介绍了Linq To Sql 'Where Or' 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一个查询来检查一个字段(字符串)是否包含一个或多个在运行时提供的词.
I need to create a query which checks if a field (string) contains one or more words supplied at run time.
基本上我需要能够提出 WhereOr 问题.这似乎应该是处理 LinqToSql 时的常见问题.
Basically I need to be able to ask a WhereOr question. This seems like it should be a common issue when dealing with LinqToSql.
我发现了以下 reference 但是无法理解它 - 也不知道如何在我的项目中使用它.
I found the following reference but can't make sense out of it - and have no idea how to use it in my project.
我尝试了以下循环:
var query = from d in context.Domains select d;
for (int i = 0; i < words.Length; i++)
{
query = query.Where(d => d.Name.Contains(words[i]));
}
但这会使用WHERE AND 子句NOT Where OR
but this builds a SQL query with WHERE AND Clauses NOT Where OR
推荐答案
我使用 PredicateBuilder 这类事情.
谓词结构如下所示:
var query = from d in context.Domains select d;
var predicate = PredicateBuilder<Domains>.False();
for (int i = 0; i < words.Length; i++)
{
predicate = predicate.Or(d => d.Name.Contains(words[i]));
}
query = query.Where(predicate);
这篇关于Linq To Sql 'Where Or' 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文