Reputation: 4171
What is the best way to merge only the keys of two dictionaries?
For example we have:
private readonly IDictionary<string, string> _dictionary1= new Dictionary<string, string>{{"1","one"},{"2","two"}};
private readonly IDictionary<string, string> _dictionary2 = new Dictionary<string, string>(){{"3","three"}};
What I want to receive is the list containing {"1","2","3"}.
Upvotes: 1
Views: 149
Reputation: 30115
Just combine to Key
arrays:
var a = _dictionary1.Keys.Union(_dictionary2.Keys);
Upvotes: 3
Reputation: 62504
UNIQUE keys using Enumerable.Union() method:
var mergedKeys = _dictionary1.Keys.Union(_dictionary2.Keys);
ALL keys using Enumerable.Concat() method:
var mergedKeys = _dictionary1.Keys.Concat(_dictionary2.Keys);
Upvotes: 1