Roman
Roman

Reputation: 8231

Convert list of Tuples to Dictionary

How to convert in the shortest way a list of Tuples to Dictionary (C#) ?

IList<Tuple<long, int>> applyOnTree = getTuples();

Upvotes: 27

Views: 18695

Answers (2)

user27414
user27414

Reputation:

Assuming the long is the key and the int is the value;

applyOnTree.ToDictionary(x => x.Item1, x => x.Item2);

Obviously, just reverse those two if it's the other way around.

Upvotes: 47

tukaef
tukaef

Reputation: 9214

Use ToDictionary extension method:

var dictionary = applyOnTree.ToDictionary(l => l.Item1, l => l.Item2);

Upvotes: 5

Related Questions