var Students = new Dictionary<string, int>();
Students.Add( "Bob" , 60);
Students.Add( "Jim" , 62);
Students.Add( "Jack", 75);
Students.Add( "John", 81);
Students.Add( "Matt", 60);
Students.Add( "Jill", 72);
Students.Add( "Eric", 83);
Students.Add( "Adam", 60);
Students.Add( "Gary", 75);
var MyVar = Students.GroupBy(r=> r.Value)
.ToDictionary(t=> t.Key, t=> t.Select(x=> x.Key));
The Students
object has Name
and Weight
key-value pairs.
In the ToDictionary method, t
variable is of type IEnumerable<IGrouping<K, T>>
. That is, IEnumerable<IGrouping<int, Students>>
Why are the Key
values returned by t=>t.Key
and t=> t.Select(**x=> x.Key**)
different? They both use the same t
variable. Key
should be of type int
.
The image was taken after the GroupBy method had been executed.(It is not the full image) One of the Key
has the value of 60
and the other one has the values of Bob
, Matt
and Adam
.