Resolved Select from List Dictionary

beantownace

Member
Joined
Feb 15, 2019
Messages
21
Programming Experience
5-10
Hello all,

I have a List<string> that contains a list of Keys I want to select from a List<Dictionary<string, object>>() how can I do this?

Example:
var fields = new List<string>();
fields.Add("CustomerID");
fields.Add("Location");

var dictionary = new List<Dictionary<string, object>>();

//How can I get from the dictionary only the key value pairs
//where the key string is in the fields list?
 
Solution
query syntax:
var selection = from d in dictionary from item in d where fields.Contains(item.Key) select item;
method syntax:
var selection = dictionary.SelectMany(d => d.Where(item => fields.Contains(item.Key)));

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,197
Location
Norway
Programming Experience
10+
query syntax:
var selection = from d in dictionary from item in d where fields.Contains(item.Key) select item;
method syntax:
var selection = dictionary.SelectMany(d => d.Where(item => fields.Contains(item.Key)));
 
Solution
Top Bottom