How to check if any words in a list contain a partial string?

http://stackoverflow.com/questions/2277444/how-to-check-if-any-words-in-a-list-contain-a-partial-string

You can use the Enumerable.Any method:

bool contained = alist.Any( l => l.Contains("somestring"));

This is checking each element using String.Contains, which checks substrings. You previously were using ICollection<string>.Contains(), which checks for a specific element of the list.

shareedit

 

http://stackoverflow.com/questions/9032655/check-if-a-string-within-a-list-contains-a-specific-string-with-linq

I think you want Any:

if (myList.Any(str => str.Contains("Mdd LH")))

It’s well worth becoming familiar with the LINQ standard query operators; I would usually use those rather than implementation-specific methods (such as List<T>.ConvertAll) unless I was really bothered by the performance of a specific operator. (The implementation-specific methods can sometimes be more efficient by knowing the size of the result etc.)

shareedit
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s