How can I do a “like” to find a dictionary key? I’m currently doing:
mydict.ContainsKey(keyName);
But some keyNames have an additional word appended (separated by a space), I’d like to do a “like” or .StartsWith(). The comparisons will look like this:
"key1" == "key1" //match
"key1" == "key1 someword" //partial match
I need to match in both cases.
You can use LINQ to do this.
Here are two examples:
bool anyStartsWith = mydict.Keys.Any(k => k.StartsWith("key1"))bool anyContains = mydict.Keys.Any(k => k.Contains("key1"))It is worth pointing out that this method will have worse performance than the .ContainsKey method, but depending on your needs, the performance hit will not be noticable.