簡體   English   中英

使用int List復制字典 <int, List<int> &gt;按價值計算

[英]Copy a dictionary with int List <int, List<int>> by value

我有一個帶有int鍵的字典,以及int Lists作為值。 我如何按價值復制?

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

Dictionary<int, List<int>> d2 = new Dictionary<int, List<int>>(d1);

您可以使用ToDictionary復制字典,使用ToList復制列表:

var dNew = d1.ToDictionary(x => x.Key, x => x.Value.ToList());

你可以使用linq:

var d2 = d1.ToDictionary(x => x.Key, y => new List<int>(y.Value));

觀看rextester的現場演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM