"分组"字典按值 [英] "Grouping" dictionary by value

查看:98
本文介绍了"分组"字典按值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典: 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());

这篇关于&QUOT;分组&QUOT;字典按值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆