"分组"字典按值 [英] "Grouping" dictionary by value
本文介绍了"分组"字典按值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字典: Dictionary< int,int>
。我想得到新词典,其中原始字典的键表示为 List< int>
。这就是我的意思:
I have a dictionary: Dictionary<int,int>
. I want to get new dictionary where keys of original dictionary represent as List<int>
. This is what I mean:
var prices = new Dictionary<int,int>();
价格
包含以下数据: / p>
The prices
contain the following data:
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());
这篇关于"分组"字典按值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文