使用int List <int,List <int >>复制字典

use*_*859 4 c# dictionary list

我有一个带有int键的字典,以及int Lists作为值.我该如何按价值复制?

我做了以下操作,但它通过引用复制了List部分:

Dictionary<int, List<int>> d2 = new Dictionary<int, List<int>>(d1);
Run Code Online (Sandbox Code Playgroud)

Fab*_*jan 5

您可以使用ToDictionary复制字典和ToList复制列表:

var dNew = d1.ToDictionary(x => x.Key, x => x.Value.ToList());
Run Code Online (Sandbox Code Playgroud)