LINQ 的通配符搜索
我想知道是否可以使用 LINQ 进行通配符搜索。
我看到 LINQ 有 Contains、StartsWith、EndsWith 等。
如果我想要 %Test if%it work% 之类的东西,我该怎么做?
问候
I would like to know if it is possible to do a wildcard search using LINQ.
I see LINQ has Contains, StartsWith, EndsWith, etc.
What if I want something like %Test if%it work%, how do I do it?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
对于
Entity Framework Core 2.0
有LIKE
运算符(2017 年 8 月宣布):For
Entity Framework Core 2.0
there isLIKE
operator (announced in August 2017):看着问题
那么我期望
字符串必须包含“Test if”和“it work”,按此顺序。
这是行不通的:
如果我使用:
那么我将找到包含“Test if”和“it work”的所有记录,但不是特别按照该顺序。
所以对于 Contains 这是不可能的。 但对于 IndexOf 来说确实如此。
IndexOf 将定位搜索字符串并返回它在字符串中的位置。 可以按照正确的顺序找到单词。
-- 更新 --
根据我最初的答案,我的目标不是提供通用解决方案,而是提供另一种不依赖于 sql 的方法的示例。 因此,原始示例仅回答字面问题是正确的。 但由于答案如果是通用的可能会更有用,因此我编写了一个 IQuerable 扩展,它允许向查询添加 like 语句,就像使用 where 语句一样简单。 该扩展适用于 Linq 和 Linq-Sql。
这将按顺序找到所有带有“Test if”和“it work”的记录。
扩展名,允许任意数量的通配符:
因为它不需要 SqlMethods,我假设您可以将其用于任何数据库,例如 MySql 或 Postgresql。 但我不确定。 我确实使用 Entity Framework 6 使用 Sql Server 对此进行了测试。上述语句在 Sql Server 中生成以下代码。
关于性能,似乎有一些关于什么是“更好”的讨论:LIKE 或 CHARINDEX。 从我读到的内容来看,CHARINDEX 似乎是我最喜欢的。
Looking at the question
then I am expecting something of
meaning that the string must contain 'Test if' and 'it work', in that order.
This will not work:
And if I use:
then I will find all records that contain both "Test if" and "it work", but not specifically in that order.
So with Contains this is not possible. But with IndexOf it is.
IndexOf will locate the searchstring AND return the position of it in the string. Making it possible to find the words in the correct order.
-- Update --
With my original answer it was not my goal to provide a generic solution, but rather an example of another approach that is not sql dependend. So it is correct that the original example only answers the literal question. But since the answer may be more useful if it is generic, I've written an IQuerable extension that allows to add a like statement to the query as easy as a where statement. The extension works for both Linq as Linq-Sql.
This will find all records with both "Test if" and "it work", in that order.
Extension, allows any number of wildcards:
Since it doesn't need SqlMethods I assume you can use this for any database, like MySql or Postgresql. But I do not know for sure. I did test this with Sql Server using Entity Framework 6. The above statement generates the following code in Sql Server.
About performance, there seems to be some discussion about what is 'better': LIKE or CHARINDEX. And from what I've read CHARINDEX seems to be favorite.
我知道这是一个老话题,但这是我非常简单的解决方案:
在这段代码中,我使用 SQL 语言的常见转义字符。
如果你想使用
*
和?
,转义字符串将相应地包含\*
和\?
,使请务必在.Replace(...)
语句中包含反斜杠字符。当然,如果您想让用户能够进行 RexEx 搜索,只需不要转义模式字符串即可。
搜索正则表达式教程以获取其他选项。
我相信通常
%
将匹配至少一个字符,而正则表达式.*
将匹配零个或多个字符。 所以实际上,%
通配符更像是.+
(贪婪)而不是.*
(惰性)。希望这可以帮助。
I know this is and old topic, but here is my very simple solution:
In this code, I am using common escape characters for the SQL language.
If you want to use say
*
and?
, escaped string will contain\*
and\?
correspondingly, make sure to include the backslash character in the.Replace(...)
statement(s).Of course, if you want to give your user the ability to RexEx search, just don't escape the pattern string.
Search Regex tutorial for other options.
I believe normally
%
will match at least one character, while the RegEx.*
will match zero or more characters. So in reality, the%
wildcard is more like.+
(greedy) rather than.*
(lazy).Hope this helps.
适用于 Linq to SQL 和内存中的 Linq。
Will work for both Linq to SQL and Linq in memory.
不确定你是在谈论 LinqToSql 还是只是 linq...但你可以像这样的正则表达式:
not sure if you talk LinqToSql or just linq... but you could regular expressions like this:
在包括 LINQ to Objects 的 .Net 代码中,我使用线程 使用 Regex 创建 SQL 的“like”like 函数。。
使用示例
更多详细信息请参阅我的帖子
SQL 的“like” .Net 中要比较的模式
In .Net code including LINQ to Objects, I am using implementation of IsSqlLikeMatch function from thread Using Regex to create a SQL's "like" like function..
Example of use
More details in my post
SQL's "like" patterns to compare in .Net
您还可以使用“包含”
You can also use "contains"
您说的是 LINQ to object 还是 LINQ to SQL?
对于 LINQ to 对象,您必须求助于正则表达式想。
Are you talking LINQ to objects or LINQ to SQL?
For LINQ to objects you'll have to resort to regular expressions me thinks.
我用它来支持用户搜索中的“*”通配符过滤器。 (顺序无关紧要):
I use this for supporting a wildcard filter of "*" in a user's search. (order does not matter):
我扩展了 Ruard van Elburg 的例子来支持我的需求,并认为我会分享。 它处理通配符,例如“a%”(startswith(a))、“%b”(endswith(b))、“a%b”(startswith(a) &&endswith(b))和“a %b%c"(startwith(a)、indexof(a)
我知道这真的很晚了,而且我知道 EFv6.2+ 支持 Like() 方法。 但也许您像我一样,在一家拥有大量遗留应用程序的小商店中,这使得简单地升级 .Net 和 EF 版本变得很困难。
I have extended Ruard van Elburg's example to support my needs, and thought I would share. It handles wildcards such as "a%" (startswith(a)), "%b" (endswith(b)), "a%b" (startswith(a) && endswith(b)), and "a%b%c" (startwith(a), indexof(a) < indexof(b), & endswith(c) ).
I understand this is really late, and I understand EFv6.2+ supports a Like() method. But maybe you are like me, in a small shop with large legacy applications that make it difficult to simply upgrade .Net and EF versions.
您可以使用 SqlMethods.Like()< /a>.
用法示例:
You can use SqlMethods.Like().
An example of the usage:
我会使用正则表达式,因为您可能并不总是使用 Linq to SQL。
就像这个 Linq to Objects 的例子
I would use Regular Expressions, since you might not always be using Linq to SQL.
Like this example of Linq to Objects
将 System.Data.Linq.SqlClient 添加到您的使用或导入列表中,然后尝试:
add System.Data.Linq.SqlClient to your using or imports list then try: