I have a Dictionary and another List. 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 not in the List<string>
.
I found this post to be helpful, Linq Query Dictionary where value in List. And was able to write the following LINQ expression.
What I have so far: Data is the dictionary and PersonList is the list of strings.
var Persons = Data.Where(kvp => !PersonList.Contains(kvp.Key))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
However, my results never actually return anything.
Update:
Dictionary<string, string> Data = new Dictionary<string, string>
{
{ "John", "aaa" },
{ "Tom", "bbb" },
{ "David", "ccc" }
};
List<string> PersonList = new List<string>
{
"Tom",
"Peter"
};
var Persons = Data.Where(kvp => !PersonList.Contains(kvp.Key))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);