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.


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


Leave a Reply

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

You are commenting using your 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