Resolved Select from List Dictionary

beantownace

Active member
Joined
Feb 15, 2019
Messages
26
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)));
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
Back
Top Bottom