c# - Merging the values of two dictionaries -
this question has reply here:
merging dictionary containing list in c# 4 answers c# merging 2 dictionaries 4 answerswas hoping inquire advice.
i have 2 dictionaries
dictionary<string, list<string>> dict = new dictionary<string, list<string>>(); dictionary<string, list<string>> dictfinal = new dictionary<string, list<string>>();
in both cases key values same both represent different lists of string values.
what want have 1 dictionary same key values , lists of string values merged in 1 dictionary
i did start serious of loops realised perhaps not best solution
foreach (var fin in dictfinal) { foreach (var valfin in fin.value) { foreach (var kvp in dict) { foreach (var v in kvp.value) { } } } }
is there improve way bringing these 2 dictionaries 'together'?
dictionary<string, list<string>> d1 = new dictionary<string, list<string>>(); dictionary<string, list<string>> d2 = new dictionary<string, list<string>>(); d2.tolist().foreach(x => { if (d1.containskey(x.key)) d1[x.key].addrange(x.value); else d1.add(x.key, x.value); });
that merge both dictionaries d1
.
without tolist()
foreach(var x in d1) { if (d1.containskey(x.key)) d1[x.key].addrange(x.value); else d1.add(x.key, x.value); }
c# dictionary
No comments:
Post a Comment