I have a Dictionary<ulong,Terminal>
called TeminalDictionary
in my program, where Terminal
has the next props:
public class Terminal
{
public string TAC { get; set; }
public string Name { get; set; }
public string Model { get; set; }
public string OS { get; set; }
public string Manufacturer { get; set; }
public uint Freq { get; set; }
}
So, for that Dictionary, I want to make another one Dictionary<string,ulong>
suming all the frequencies for every Manufacturer, so the dictionary key is Terminal.Manufacturer
and the value is Sum(Terminal.Freq)
with the same Manufacturer.
I tried this expression:
var pieDictionary = TerminalDictionary
.GroupBy(x => x.Value.Manufacturer)
.ToDictionary(g => g.Sum(v => v.Value.Freq));
but it says that I'm trying to add an element with the same key, so I'm a little bit lost...
Regards!
You just need to provide the key in the new dictionary as well, which in your case is the key of the group. Given that you don't appear to need the keys within the original dictionary, I'd write this as:
var pieDictionary = TerminalDictionary.Values
.GroupBy(x => x.Manufacturer)
.ToDictionary(g => g.Key, g => g.Sum(v => v.Freq));
See more on this question at Stackoverflow