Use ToDictionary ?
orignalDictionary.ToDictionary(kp => kp.Value, kp => kp.Key);
This works because IDictionary<TKey,TElement>
; is also an IEnumerable<KeyValuePair<TKey,TElement>>
;. Just be aware that if you have duplicate values, you will get an exception.
In case you have duplicate values, you will need to decide on what to do with them. One simple way would be to ignore duplicates by grouping on Value first, then make the dictionary.
originalDictionary
.ToLookup(kp => kp.Value)
.ToDictionary(g => g.Key, g => g.First().Key);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…