c# - How to get Key, Value from dictionary when value is a list -
so have dictionary have key, , value list contains strings.. example..
{"key1", list1} list1 = [value 1 value2, value3]
and have multiple keys , therefore values have lists. want display shows
key1: value1 key1: value2 key1: value3
so want able display key , value. unsure of how that.
foreach (var value in foodcategory_item.values) { foreach(var item in value) { console.writeline("value of dictionary item is: {0}", item); } }
that's have far, iterate on values, know how do, not sure how key values in there well, because iterate on values first, , iterate through key items..
this should work:
foreach (keyvaluepair<string, list<string>> kvp in foodcategory_item) { foreach (string item in kvp.value) { console.writeline ("{0}: {1}", kvp.key, item); } }
dictionary
implements ienumerable<keyvaluepair<tkey, tvalue>>
, can iterate on directly.
Comments
Post a Comment