簡體   English   中英

將字典轉換為元組列表

[英]Convert Dictionary to a list of Tuples

如何以最短的方式將字典轉換為元組列表(C#)?

Dictionary<long,int> myDico = new Dictionary<long,int>();
IList<Tuple<long, int>> applyOnTree = myDico.Something();

像這樣的東西?

var list = myDico.Select(x => new Tuple<long, int>(x.Key, x.Value)).ToList();

你可以試試這個

var applyOnTree = myDico.Select(x => Tuple.Create(x.Key, x.Value)).ToList();

System.ValueTuple (struct, mutable field) 是大多數時候的方法。 System.Tuple (class, readonly) 不太有趣。

List<(long,int)> tuples = new Dictionary<long, int>().Select(x => (x.Key, x.Value)).ToList();

參考: https : //docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples

暫無
暫無

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

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