I have a list generated from LINQ query. An example of this list is the following
ID = 1, CODE = 2
ID = 1, CODE = 3
ID = 1 CODE = 4
....
I would like to fill a dictionary with these values to have the key of my dictionary equals to id of my list and the value like the code of my list
Wxample what I want is
myDict = 1 : {2,3,4}
Is it possible?
Thank you in advance!
You can use GroupBy
:
Dictionary<int, List<int>> myDict = list
.GroupBy(x => x.ID)
.ToDictionary(g => g.Key, g => g.Select(x => x.CODE).ToList());