[英]Linq to SQL: Partial matching strings using contains on an array of strings
Here is my code: 这是我的代码:
string[] customerNames = searchModel.CustomerName.Split(',');
query = query.Where(d => customerNames.Contains(d.CustomerName, comparer) || customerNames.Contains(d.Company1.CompanyName, comparer));
Which works if you are just looking for exact matches. 如果您只是在寻找完全匹配的内容,则可以使用。 However I'd like to partial match, ie: if customerNames contains an element
'ell'
, it would select d if d.CustomerName
was 'Hello'
since 'ell' is in 'Hello' 但是我想部分匹配,即:如果customerNames包含元素
'ell'
,则如果d.CustomerName
是'Hello'
,它将选择d,因为'ell'在'Hello'中
I tried overriding the EqualityComparer, but I believe it is trying to use the GetHashCode
function rather than the Equals
in the Comparer, which I'm not sure how to implement. 我尝试覆盖EqualityComparer,但我相信它正在尝试使用
GetHashCode
函数而不是Comparer中的Equals
,我不确定该如何实现。
How should I go about this? 我应该怎么做?
string[] customerNames = searchModel.CustomerName.Split(',');
query = query.Where(d => customerNames.Any(c => c.Contains(d.CustomerName)) || customerNames.Any(c => c.Contains(d.Company1.CompanyName)));
But you should be aware, that it may get really slow when customerNames
has a lot of items. 但是您应该知道,当
customerNames
包含很多项时,它可能会变慢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.