score:1

Accepted answer

You can use some thing like this :

Dictionary<string, int> dictionary = new Dictionary<string, int> {{"A", 1}, {"B", 2}, {"C", 3}};

List<string> list = new List<string> {"A","B"};

var result = dictionary.Where(x => list.Contains(x.Key)).ToList();

or

var result = dictionary.Where(x => list.Contains(x.Key)).ToDictionary(x=>x.Key,x=>x.Value);

score:1

There are multiple ways to achieve what you need

In case all keys of D1 are present in L1 without mutating D1

Dictionary<string, string> D2 = new Dictionary<string, string>();
L1.ForEach(x => D2[x] = D1[x]);

OR

var D2 = L1.ToDictionary(el => el, key => D1[key]);

Safe option:

var D2 = D1.Keys.Intersect(L1).ToDictionary(key => key, key => D1[key]);

and even more, depends on your creativity

Note that this is slow for big list and dictionary

D1.Where(x => L1.Contains(x.Key))

Related Articles