Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search from a list inside a dictionary<string, List<string>>

if I have a dictionary <string, List<string>>. (ex. <12345, List<"ABC", "456", "123">> and I want to pull out the key '12345' where I have "456" in the list of strings for each entry in the list. So my result would be another list. Wouldn't this be done with a linq statement?

like image 555
user1186050 Avatar asked May 29 '26 04:05

user1186050


1 Answers

Wouldn't this be done with a linq statement?

Sure. It won't be efficient, but it's pretty simple:

var input = "456";
var matchingKeys = dictionary.Where(kvp => kvp.Value.Contains(input))
                             .Select(kvp => kvp.Key);

If you want it to be efficient as well, you should store the reverse mapping too, and update both together.

like image 131
Jon Skeet Avatar answered May 31 '26 19:05

Jon Skeet