C# LINQ 的通配符搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1040380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:45:17  来源:igfitidea点击:

Wildcard search for LINQ

c#sqllinqwildcard

提问by PlayKid

I would like to know if it is possible to do a wildcard search using LINQ.

我想知道是否可以使用 LINQ 进行通配符搜索。

I see LINQ has Contains, StartsWith, EndsWith, etc.

我看到 LINQ 有包含、StartsWith、EndsWith 等。

What if I want something like %Test if%it work%, how do I do it?

如果我想要 %Test if%it work% 之类的东西怎么办?

Regards

问候

采纳答案by David Basarab

I would use Regular Expressions, since you might not always be using Linq to SQL.

我会使用正则表达式,因为您可能并不总是使用 Linq to SQL。

Like this example of Linq to Objects

像这个 Linq to Objects 的例子

List<string> list = new List<string>();
list.Add("This is a sentence.");
list.Add("This is another one.");
list.Add("C# is fun.");
list.Add("Linq is also fun.");

System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex("This");

var qry = list
    .Where<string>(item => regEx.IsMatch(item))
    .ToList<string>();

// Print results
foreach (var item in qry)
{
    Console.WriteLine(item);
}

回答by Ryan Versaw

You can use SqlMethods.Like().

您可以使用SqlMethods.Like()

An example of the usage:

用法示例:

var results =
        from u in users
        where SqlMethods.Like(u.FirstName, "%John%")
        select u;

回答by Rony

.Where( column LIKE "Pattern")

回答by fretje

Are you talking LINQ to objects or LINQ to SQL?

您是在谈论 LINQ 到对象还是 LINQ 到 SQL?

For LINQ to objects you'll have to resort to regular expressionsme thinks.

对于 LINQ to objects,您将不得不求助于我认为的正则表达式

回答by bytebender

not sure if you talk LinqToSql or just linq... but you could regular expressions like this:

不确定你是在谈论 LinqToSql 还是只是 linq ......但你可以像这样的正则表达式:

.Where(dto => System.Text.RegularExpressions.Regex.IsMatch(dto.CustomerName, @"Ad"));

回答by Joe Davis

add System.Data.Linq.SqlClient to your using or imports list then try:

将 System.Data.Linq.SqlClient 添加到您的使用或导入列表中,然后尝试:

var results= from x in data
             where SqlMethods.Like(x.SearchField, “%something%like%this%”)
             select x;

回答by Gerhard

var result = (from x in db.Members
              where x.IDNumber.Contains(idnumber)
              && x.InstitutionIdentifier == institution.Identifier
              select x).ToList();
return result;

Will work for both Linq to SQL and Linq in memory.

将适用于 Linq to SQL 和内存中的 Linq。

回答by Michael Freidgeim

In .Net code including LINQ to Objects, I am using implementation of IsSqlLikeMatchfunction from thread Using Regex to create a SQL's "like" like function..

在包括 LINQ to Objects 的 .Net 代码中,我使用 IsSqlLikeMatch函数从线程Using Regex to create a SQL's "like" like function 的实现。.

Example of use

使用示例

bool ret = message.IsSqlLikeMatch(pattern);

More details in my post SQL's "like" patterns to compare in .Net

在我的帖子SQL 的“喜欢”模式中有更多细节 可以在 .Net 中进行比较

回答by spadelives

You can also use "contains"

您也可以使用“包含”

var myresult = db.MyItems.Where(x=>x.MyField.Contains(mysearchstring));

回答by nurchi

I know this is and old topic, but here is my very simple solution:

我知道这是一个老话题,但这是我非常简单的解决方案:

string s=Regex.Escape("pattern - escaped for sanity").Replace("%", ".*").Replace("_", ".?");
user => Regex.IsMatch(user.FullName, s, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

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 backslashcharacter 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.

在这段代码中,我使用了 SQL 语言的常见转义字符。如果你想使用的发言权*?,逃过字符串将包含\*\?相应的,一定要包括反斜杠在字符.Replace(...)声明(S)。当然,如果您想让您的用户能够进行 RexEx 搜索,只需不要对模式字符串进行转义。

Search Regex tutorial for other options.

搜索 Regex 教程以获取其他选项。

I believe normally %will match at least one character, while the RegEx .*will match zero or morecharacters. So in reality, the %wildcard is more like .+(greedy) rather than .*(lazy).

我相信通常%会匹配至少一个字符,而正则表达式.*会匹配零个或多个字符。所以实际上,%通配符更像是.+(贪婪)而不是.*(懒惰)。

Hope this helps.

希望这可以帮助。