Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Dictionary as below
Dictionary <int, string>  dct= new Dictionary <int, string>();
dct.Add(1,"group1")
dct.Add(2,"group1")
dct.Add(3,"group1")
dct.Add(4,"group1")
dct.Add(5,"group1") 
dct.Add(8,"group2")
dct.Add(9,"group2")
dct.Add(10,"group2")
dct.Add(11,"group2")
dct.Add(12,"group2")

I need to have dictionary<string, List<int>> which will have two records, first record would be group1 as key and [1,2,3,4,5] as value in List<int> and second record would be group2 as key and [8,9,10,11,12] as value in List<int>
I tried with below code but it does not resturn me as expected.
  var _setsGroup = dct.Select(t => new { t.Key, t.Value }).ToDictionary(t => t.Key, t => t.Value).GroupBy(t => t.Value);
Thanks in advance 


What I have tried:

var _setsGroup = dct.Select(t => new { t.Key, t.Value }).ToDictionary(t => t.Key, t => t.Value).GroupBy(t => t.Value);
Posted
Updated 11-Feb-20 1:29am
v3

1 solution

Something like this should work:
C#
Dictionary<string, List<int>> setsGroups = dct
    .GroupBy(kvp => kvp.Value)
    .ToDictionary(grp => grp.Key, grp => grp.Select(kvp => kvp.Key).ToList());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900