rameshKumar1717 Ответов: 1

Использование C# linq для извлечения записей в словарь


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 


Что я уже пробовал:

var _setsGroup = dct.Select(t => new { t.Key, t.Value }).ToDictionary(t => t.Key, t => t.Value).GroupBy(t => t.Value);

1 Ответов

Рейтинг:
7

Richard Deeming

Что-то вроде этого должно сработать:

Dictionary<string, List<int>> setsGroups = dct
    .GroupBy(kvp => kvp.Value)
    .ToDictionary(grp => grp.Key, grp => grp.Select(kvp => kvp.Key).ToList());