Yurii Hohan
Yurii Hohan

Reputation: 4171

Merging the keys of two dictionaries

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

Answers (3)

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56172

Look at Concat and Union methods.

Upvotes: 1

Samich
Samich

Reputation: 30115

Just combine to Key arrays:

var a = _dictionary1.Keys.Union(_dictionary2.Keys);

Upvotes: 3

sll
sll

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

Related Questions