Reputation: 3
dictionary1
looks like this
[DATA_WZD],[WERKZEUG],NUMM=1,GRUP=1,AKR=0,MODE=1,MAGA=1,LAGE=0,DURC=348.2,........NOTCH=130,MANCH=0,RED3=0,NAME="Querschnittblatt 350-3,5mm ",BEME="Bemerkung "
[WERKZEUG],NUMM=2,GRUP=1,MAKR=0,MODE=0,MAGA=0,LAGE=0,DURC=449.6........NOTCH=150,MANCH=0,RED3=0,NAME="Querschnittblatt 450/4,0mm ",BEME="Bemerkung "
[WERKZEUG] and so on.
dictionary2
looks similar, but has extra keys and values
[DATA_WZD]
[WERKZEUG],NUMM=1,GRUP=1,MAKR=0,MODE=0,MAGA=0,LAGE=0,DURC=385.8,.......NOTCH=130,MANCH=0,RED3=0,CYCLE=0,CMIN=0,CMAX=0,ACTIVE=1,NAME="Querschnittblatt 35",BEME="Bemerkung"
[WERKZEUG],NUMM=2,GRUP=1,MAKR=0,MODE=0,MAGA=0,LAGE=0,DURC=429.6........NOTCH=150,MANCH=0,RED3=0,CYCLE=0,CMIN=0,CMAX=0,ACTIVE=1,NAME="Querschnittblatt 45",BEME="Bemerkung"
[WERKZEUG].....
I want the keys and the values from dictionary2
which are missing in dictionary1
to be inserted at the same position in dictionary2
. should than look like this
[DATA_WZD]
[WERKZEUG],NUMM=1,GRUP=1,AKR=0,MODE=1,MAGA=1,LAGE=0,DURC=348.2,........NOTCH=130,MANCH=0,RED3=0,CYCLE=0,CMIN=0,CMAX=0,ACTIVE=1,NAME="Querschnittblatt 350-3,5mm ",BEME="Bemerkung "
[WERKZEUG],NUMM=2,GRUP=1,MAKR=0,MODE=0,MAGA=0,LAGE=0,DURC=449.6........NOTCH=150,MANCH=0,RED3=0,CYCLE=0,CMIN=0,CMAX=0,ACTIVE=1,NAME="Querschnittblatt 450/4,0mm ",BEME="Bemerkung "
[WERKZEUG]......
What's a good way of doing this?
Upvotes: 0
Views: 1770
Reputation: 7313
You could try using Linq:
dict1.Keys.Except(dict2.Keys)
.ToList()
.ForEach(k => dict2.Add(k, dict1[k]));
or if you want empty strings instead of dict1 values:
dict1.Keys.Except(dict2.Keys)
.ToList()
.ForEach(k => dict2.Add(k, string.Empty));
Upvotes: 1
Reputation: 70538
Here is a simple algorithm to merge two dictionaries in pseudo-code:
Given
Dictionary dict1,dict2
Do this:
Dictionary dictRes = new Dictionary;
for each(key in dict1)
dictRes.Add(dict1(key));
for each(key in dict2)
if (not dictRes contains element)
dictRes.Add(dict2(key));
Upvotes: 0