0

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?

2
  • Similar question: stackoverflow.com/questions/3748289/… Commented Feb 10, 2014 at 22:51
  • GetHashCode is not guaranteed to prevent collisions so you can be sure there's more going on than that. Commented Feb 10, 2014 at 22:55

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

6 Comments

I just was answering this myself. I'll check you off when 8 minutes has passed. customerNames should be very few items, but yeah I'm not sure what I can do to avoid this logic.
Will it translated to sql or will executed on client side on full set?
You can use PredicateBuilder and just add list of OR conditions to your query. Which should probably be better and faster then Any+Contains chain.
@HamletHakobyan Should be transform into SQL (that's why I removed the comparer).
The expression tree would be better solution in this case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.