Linq Query Dictionary where value in List

c#, linq

Solution

Are you looking for keys or values? If you're looking for values use

var Persons = Data.Where(kvp => PersonList.Contains(kvp.Value))
                  .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

If instead you really want keys then your code should work but another option would be:

var Persons = Data.Where(kvp => PersonList.Contains(kvp.Key))
                  .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

Problem

I have a `Dictionary<string, string>` and another `List<string>`. What I am trying to achieve is a linq query to get all items out of the dictionary where any values from said dictionary are in the `List<string>`. I found this post to be helpful, LINQ querying a Dictionary against a List . And was able to write the following linq expression, however my results never actually return anything. What I have so far. `Data` is the dictionary and `PersonList` is the list of strings. ``` var Persons = PersonList.Where(x => Data.ContainsKey(x)) .Select(z => new { key = z, value = Data[z] }) .ToList(); ```

Original source

Related problems