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);
LINQ selectの助けを借りて、すべての"valueTitle"を取得することは可能ですか?
UPDATE: 申し訳ありませんが、最初に書くべきでした。subListからではなくexitDictionaryから結果を取得する必要があります。
fieldname1`の値で検索している場合は、次のようにしてみてください。
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();
subDictionaryの値の型(
Dictionary<string, object>` を明示的に)で検索する場合は、次のようにします。
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();
どちらの選択肢も返ってきます。
title1
title2
title3
title1
title2
title3