C# Linq到SQL:在字符串数组上使用contains进行部分匹配字符串

C# Linq到SQL:在字符串数组上使用contains进行部分匹配字符串,c#,.net,linq,linq-to-sql,iequalitycomparer,C#,.net,Linq,Linq To Sql,Iequalitycomparer,这是我的密码: string[] customerNames = searchModel.CustomerName.Split(','); query = query.Where(d => customerNames.Contains(d.CustomerName, comparer) || customerNames.Contains(d.Company1.CompanyName, comparer)); 如果你只是在寻找精确的匹配项,这是可行的。但是,我想进行部分匹配,即:如果Cu

这是我的密码:

string[] customerNames = searchModel.CustomerName.Split(',');
 query = query.Where(d => customerNames.Contains(d.CustomerName, comparer) || customerNames.Contains(d.Company1.CompanyName, comparer));
如果你只是在寻找精确的匹配项,这是可行的。但是,我想进行部分匹配,即:如果CustomerName包含一个元素
'ell'
,它将在
d时选择d。CustomerName
'Hello'
,因为'ell'在'Hello'中

我试图重写EqualityComparer,但我相信它试图使用
GetHashCode
函数,而不是比较器中的
Equals
,我不知道如何实现

我该怎么做

string[] customerNames = searchModel.CustomerName.Split(',');
query = query.Where(d => customerNames.Any(c => c.Contains(d.CustomerName)) || customerNames.Any(c => c.Contains(d.Company1.CompanyName)));

但是您应该注意,当
客户名称
有很多项时,它可能会变得非常慢。

类似的问题:GetHashCode不能保证防止冲突,所以您可以确定会发生更多的事情。我只是自己回答这个问题。8分钟后我会给你结账。customerNames应该是很少的项,但我不确定我能做些什么来避免这种逻辑。它会转换为sql还是会在客户端完整地执行?您可以使用,只需将
条件列表添加到查询中即可。它可能比任何
+
包含的
链更好更快。@HamletHakobyan应该转换为SQL(这就是我删除比较器的原因)。在这种情况下,表达式树将是更好的解决方案。