Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all values in a Dictionary<string, object> where key contains substring

Let's say I have a dictionary declared as follow:

Dictionary<string, IData> map;

I want to get all the values with the keys containing a specific substring with for example a function like

public IEnumerable<IData> GetContains(string pattern) {}

And I figured how to obtain a list of keys matching the pattern using

var result = mapData.Keys.Where(a => a.Contains(pattern)).ToArray()

but I cannot figure out how to reuse the returned keys to get all the corresponding values in one query.

like image 930
kittikun Avatar asked Dec 07 '22 00:12

kittikun


1 Answers

You can use Where on your Dictionary instead of the Keys collection then get the values with Select:

mapData.Where(kvp => kvp.Key.Contains(pattern)).Select(kvp => kvp.Value);
like image 50
Selman Genç Avatar answered Dec 10 '22 13:12

Selman Genç