简体   繁体   中英

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'

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM