特定单词的LINQ不起作用

本文关键字:LINQ 不起作用 单词 | 更新日期: 2023-09-27 18:21:14

UPDATE:这是我正在使用的确切代码,而不是给出一个基本的示例。

query = context.GetQueryable<SearchCustomers>() // This is a huge list of customers
 if (!string.IsNullOrEmpty(company))
                        query = query.Where(x => x.CompanyName.Contains(company));

公司名称为Mike and Joe Repair Shop

如果我只搜索"Mike",它会按预期返回Mike and Joe Repair Shop。如果我搜索"Mike and Joe Repair Shop",它会返回Null。如果我搜索"Joe Repair Shop",它会返回预期的Mike and Joe Repair Store。

我猜这与单词"and"在字符串中有关。

我遇到了一个问题,如果查询要查找的单词包含单词"and",它就会失败。情况如下:

var word = "Cats and Dogs"
query = word.contains("Cats and Dogs")
return query
//This returns NULL
if i do query = word.contains("Cat")
return query
//This returns Cat and Dog
if i do query = word.contains("Cat And")
return query
//This returns Cat and Dog

我需要摆脱"answers"这个词吗?

特定单词的LINQ不起作用

string.Contains()方法仅用于匹配字符串的子字符串。因此,当使用Contains()方法时,它不是保留关键字。我尝试了两种不同的方法来测试您关于and:的断言

List<string> phrases = new List<string>() {"Cats and Dogs", "Cats and Bats", "Dogs and Trees"};
IEnumerable<string> f = phrases.Where<string>(x =>x.Contains("Cats and Dogs"));
System.Console.WriteLine(f.FirstOrDefault());
> Cats and Dogs

再次:

List<string> phrases = new List<string>() {"Cats and Dogs", "Cats and Bats", "Dogs and Trees"};
IEnumerable<string> g = from phrase in phrases where phrase.Contains("Cats and Dogs") select phrase;
System.Console.WriteLine(g.FirstOrDefault())
> Cats and Dogs

我的最佳猜测是,在原始的CompanyName中有unicode空间,它与带有ascii空白的字符串的Contains()不匹配。

例如:

System.Console.WriteLine("Cats and Dogs");
> Cats and Dogs
System.Console.WriteLine("Cats and'u2000Dogs");
> Cats and Dogs
System.Console.WriteLine("Cats and'u2000Dogs".Contains("Cats and Dogs"));
> False