Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何以最短的方式将元组列表转换为字典(C#)?
IList<Tuple<long, int>> applyOnTree = getTuples();
假设long是键,int是值;
long
int
applyOnTree.ToDictionary(x => x.Item1, x => x.Item2);
显然,如果相反,只需将这两个颠倒即可。
使用ToDictionary扩展方法:
ToDictionary
var dictionary = applyOnTree.ToDictionary(l => l.Item1, l => l.Item2);