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.
我找到了以下参考,但是无法理解它-并且不知道如何在我的项目中使用它.
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屋!
查看全文