Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a dictionary with int List <int, List<int>> by value

I have a dictionary with int keys, and int Lists as values. How can I copy this by value?

I did the following, but it copies the List part by reference:

Dictionary<int, List<int>> d2 = new Dictionary<int, List<int>>(d1);
like image 395
user3001859 Avatar asked Jan 03 '23 08:01

user3001859


1 Answers

You could use ToDictionary to copy the dictionary and ToList to copy lists:

var dNew = d1.ToDictionary(x => x.Key, x => x.Value.ToList());
like image 136
Fabjan Avatar answered Jan 05 '23 00:01

Fabjan