Check if dictionary contains substring?

john cs picture john cs · Mar 16, 2013 · Viewed 12.5k times · Source

Say I have two dictionaries with the following key value pairs:

1, "Hello"
2, "Example"

And another dictionary as follows:

1, "HelloWorld"
2, "Example2"

I want to find out if these dictionaries contain the substring "hello" within them.
dictionary.ContainsValue("Hello") will work for the first example but not the second one. How can I check for existence of a substring in all values in a dictionary?

Answer

juharr picture juharr · Mar 16, 2013

Just use Any to to check for the first Value that contains "Hello"

dictionary.Any(kvp=>kvp.Value.Contains("Hello"))