39

I have next dictionary in C#

Dictionary<string, object> subDictioanry = new Dictionary<string, object>(); List<Dictionary<string, string>> subList = new List<Dictionary<string, string>>(); subList.Add(new Dictionary<string, string>(){ {"valueLink", "link1"}, {"valueTitle","title1"} }); subList.Add(new Dictionary<string, string>(){ {"valueLink", "link2"}, {"valueTitle","title2"} }); subList.Add(new Dictionary<string, string>(){ {"valueLink", "link3"}, {"valueTitle","title3"} }); subDictioanry.Add("title", "title"); subDictioanry.Add("name", "name"); subDictioanry.Add("fieldname1", subList); Dictionary<string, object> exitDictionary = new Dictionary<string, object>(); exitDictionary.Add("first", subDictioanry); exitDictionary.Add("second", subDictioanry); 

Is it possible to get all "valueTitle" with help of LINQ select?

UPDATE: Sorry, i should write it first - i need to get result from exitDictionary, not from subList

1
  • 1
    This would be way easier to reason about if you used classes instead of dictionaries. Just saying. Commented Nov 1, 2013 at 12:03

4 Answers 4

34

If you are searching by the fieldname1 value, try this:

var r = exitDictionary .Select(i => i.Value).Cast<Dictionary<string, object>>() .Where(d => d.ContainsKey("fieldname1")) .Select(d => d["fieldname1"]).Cast<List<Dictionary<string, string>>>() .SelectMany(d1 => d1 .Where(d => d.ContainsKey("valueTitle")) .Select(d => d["valueTitle"]) .Where(v => v != null)).ToList(); 

If you are looking by the type of the value in the subDictionary (Dictionary<string, object> explicitly), you may do this:

var r = exitDictionary .Select(i => i.Value).Cast<Dictionary<string, object>>() .SelectMany(d=>d.Values) .OfType<List<Dictionary<string, string>>>() .SelectMany(d1 => d1 .Where(d => d.ContainsKey("valueTitle")) .Select(d => d["valueTitle"]) .Where(v => v != null)).ToList(); 

Both alternatives will return:

title1 title2 title3 title1 title2 title3 
Sign up to request clarification or add additional context in comments.

Comments

12

One way would be to first flatten the list with a SelectMany:

subList.SelectMany(m => m).Where(k => k.Key.Equals("valueTitle")); 

Comments

3

This will return all the values matching your key valueTitle

subList.SelectMany(m => m).Where(kvp => kvp.Key == "valueTitle").Select(k => k.Value).ToList(); 

1 Comment

This is incorrect. kvp would not be a KeyValuePair, it would be a Dictionary<string, string>. You are missing a SelectMany to flatten the list.
1
var res = exitDictionary .Select(p => p.Value).Cast<Dictionary<string, object>>() .SelectMany(d => d) .Where(p => p.Key == "fieldname1") .Select(p => p.Value).Cast<List<Dictionary<string,string>>>() .SelectMany(l => l) .SelectMany(d=> d) .Where(p => p.Key == "valueTitle") .Select(p => p.Value) .ToList(); 

This also works, and easy to understand.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.