c# - "Grouping"字典值

标签 c# .net linq dictionary grouping

我有一本字典:Dictionary<int,int> .我想获得新字典,其中原始字典的键表示为 List<int> .这就是我的意思:

var prices = new Dictionary<int,int>();

prices包含以下数据:

1   100
2   200
3   100
4   300

我想得到 IList<Dictionary<int,List<int>>> :

int      List<int>
100      1,3
200      2
300      4

我该怎么做?

最佳答案

var prices = new Dictionary<int, int>();
prices.Add(1, 100);
prices.Add(2, 200);
prices.Add(3, 100);
prices.Add(4, 300);

Dictionary<int,List<int>> test  = 
                   prices.GroupBy(r=> r.Value)
                  .ToDictionary(t=> t.Key, t=> t.Select(r=> r.Key).ToList());

关于c# - "Grouping"字典值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410590/

相关文章:

c# - 线程退出时如何处理 ThreadLocal 值?

.net - 映射网络驱动器时的自定义身份验证 - 这可能吗?

c# - ASP.NET 5 : Access-Control-Allow-Origin in response

c# - UWP 网格过渡

C# Linq 内部连接

c# - 确定堆上对象的对象分配发生的位置

c# - 在 C# 中的给定变量之间的表中查找最小值和最大值

c# - TakeWhile,但我也想要输入序列的其余部分

c# - 您将如何重构此 LINQ 代码?

c# - 如何将多个表中的数据显示到dataGridView