更新:以下是我使用的确切代码,而非提供基本示例。
query = context.GetQueryable<SearchCustomers>() // This is a huge list of customers
if (!string.IsNullOrEmpty(company))
query = query.Where(x => x.CompanyName.Contains(company));
公司名称是Mike和Joe Repair Shop
如果我只是搜索&#34; Mike&#34;它按预期返回Mike和Joe Repair Shop。如果我搜索&#34; Mike和Joe Repair Shop&#34;它返回Null。如果我搜索&#34; Joe Repair Shop&#34;它返回了预期的Mike和Joe Repair Shop。
我猜它与“&#34;和&#34;”这个词的关系有关。在字符串中。
我遇到一个问题,如果查询所查找的单词包含单词&#34;和&#34;它失败。情况如下:
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
我是否需要逃避&#34;和&#34;字?
答案 0 :(得分:1)
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