LINQ问题(包含) [英] LINQ Question (Contains)
问题描述
我最近偶然发现了一个非常有趣的LINQ to SQL问题和
奇怪,是否有人可能有答案。 (我正在做一些
在德国增加LINQ福音传播。)。
假设我想从数据库中选择行并检查是否特定的
列包含关键字列表中的关键字。以下作品只需
罚款:
列表< stringsearchTerms =新列表< string>(){" Maria",
佩德罗 };
var query =来自db.Customers中的c
其中searchTerms.Contains(c.ContactName)
select c ;
dataGridView1.DataSource = query;
此代码存在的问题是,c .ContactName必须完全匹配
" Maria"或佩德罗。它不包括子字符串搜索,所以给定一个
ContactName可能是Maria Foo。或Pedro Bar或Pedro Bar。它不会返回那些
行。任何关于如何实现这一点的想法,而不是在
a foreach循环中迭代查询?
旁注:Visual Studio 2008 IntelliSense表明有一个
包含<重载,它接受IEqualityComparer类型。
但是,以下代码编译良好但抛出运行时异常:
SubstringComparer substringComparer = new SubstringComparer();
List< stringsearchTerms = new List< string>(){" Maria",
" Pedro" };
var query =来自db.Customers中的c
其中searchTerms.Contains< string>(c.ContactName,
substringComparer)
选择c;
任何想法如何声明一个约束,该约束返回包含
关键字的行收藏?
-
-------------------------- -----
http://www.24100.net
I recently stumbled across a pretty interesting LINQ to SQL question and
wonder, whether anybody might have an answer. (I''m doing quite some
increasing LINQ evangelism down here in Germany.).
Assume I want to select rows from a database and check whether a specific
column contains keywords from a list of keywords. The following works just
fine:
List<stringsearchTerms = new List<string>() { "Maria",
"Pedro" };
var query = from c in db.Customers
where searchTerms.Contains(c.ContactName)
select c;
dataGridView1.DataSource = query;
The problem with this code is, that c.ContactName has to match exactly
"Maria" or "Pedro". It does not include substring search, so given a
ContactName might be "Maria Foo" or "Pedro Bar" it does not return those
rows. Any idea as to how to achieve this without iterating through query in
a foreach loop?
Sidenote: Visual Studio 2008 IntelliSense indicates that there is a
Contains<overload which accepts an IEqualityComparer type.
However, the following code compiles fine but throws a runtime exception:
SubstringComparer substringComparer = new SubstringComparer();
List<stringsearchTerms = new List<string>() { "Maria",
"Pedro" };
var query = from c in db.Customers
where searchTerms.Contains<string>(c.ContactName,
substringComparer)
select c;
Any idea how to declare a constraint which returns rows which contain
keywords contained within a collection?
--
-------------------------------
http://www.24100.net
推荐答案
Ralf Rottmann( www.24100.net )< be ***** @ live.comwrote:
Ralf Rottmann (www.24100.net) <Be*****@live.comwrote:
我最近偶然发现了一个非常有趣的LINQ to SQL问题和
想知道,是否有人可能有答案。 (我正在做一些
在德国增加LINQ传福音。)
I recently stumbled across a pretty interesting LINQ to SQL question and
wonder, whether anybody might have an answer. (I''m doing quite some
increasing LINQ evangelism down here in Germany.).
我没有答案,我很害怕,但SQL支持你想要做什么的b $ b想做什么第一名?你希望生成的SQL对于
是什么?
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet
英国的世界级.NET培训: http:// iterativetraining .co.uk
I don''t have the the answer, I''m afraid, but does SQL support what you
want to do in the first place? What would you like the generated SQL to
be?
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jon,
有趣的问题我必须承认,我问自己同样的问题:
我可以在SQL中优雅地做到这一点吗?好吧,在SQL中我可以通过OR组合多个WHERE
子句。我想知道LINQ是否支持这样的antyhing ......
" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言
新闻:MP ********************* @ msnews.microsoft.com 。 ..
Jon,
Interesting question and I gotta admit, I asked myself the same question:
Can I do elegantly it in SQL? Well, in SQL I could combine multiple WHERE
clauses by OR. I wonder whether LINQ supports antyhing like that...
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Ralf Rottmann( www.24100 .net )< be ***** @ live.comwrote:
Ralf Rottmann (www.24100.net) <Be*****@live.comwrote:
>我最近偶然发现了一个非常有趣的LINQ to SQL问题和
想知道,是否有人可能有答案。 (我正在做一些
在德国增加LINQ传福音。)
>I recently stumbled across a pretty interesting LINQ to SQL question and
wonder, whether anybody might have an answer. (I''m doing quite some
increasing LINQ evangelism down here in Germany.).
我没有答案,我很害怕,但SQL支持你想要做什么的b $ b想做什么第一名?你希望生成的SQL对于
是什么?
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet
英国的世界级.NET培训: http:// iterativetraining .co.uk
Ralf Rottmann( www.24100.net )< be ***** @ live.comwrote:
Ralf Rottmann (www.24100.net) <Be*****@live.comwrote:
有趣的问题我必须承认,我问道我自己也是同样的问题:
我可以在SQL中优雅地做到这一点吗?好吧,在SQL中我可以通过OR组合多个WHERE
子句。我想知道LINQ是否支持像这样的antyhing ...
Interesting question and I gotta admit, I asked myself the same question:
Can I do elegantly it in SQL? Well, in SQL I could combine multiple WHERE
clauses by OR. I wonder whether LINQ supports antyhing like that...
是的,我怀疑如果你做了类似
var的事情query = from db in db.Customers
where cust.Name.Contains(" Fred")
|| cust.Name.Contains(" Ginger")
select cust;
它应该可以正常工作。 (我没有测试过,我不得不说......)
-
Jon Skeet - < sk *** @ pobox .com>
http://www.pobox.com/~skeet博客: http://www.msmvps.com/jon.skeet
英国的世界级.NET培训: http:// iterativetraining。 co.uk
Yes, I suspect that if you do something like
var query = from cust in db.Customers
where cust.Name.Contains("Fred")
|| cust.Name.Contains("Ginger")
select cust;
it should work fine. (I haven''t tested it, I have to say...)
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
这篇关于LINQ问题(包含)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!