Because it's not a
IEnumerable<IGrouping<int, Student>>
It's a
IEnumerable<IGrouping<int, KeyValuePair<int, Student>>>
In t => t.Key
you're referring to the grouping key, and in Select(x => x.Key)
you're referring to the KeyValuePair
.
You can hover over the variables in Visual Studio to see the types and the type parameters.
In more detail:
// return an IEnumerable<IGrouping<int, KeyValuePair<int, Student>>>
Students.GroupBy(r=> r.Value)
// iterate over each IGrouping<int, KeyValuePair<int, Student>>
.ToDictionary(
group => group.Key,
// iterate over each KeyValuePair<int, Student> in the group
// because IGrouping<TKey, TElement> inherits from IEnumerable<TElement>
// so it's a Select over an IEnumerable<KeyValuePair<int, Student>>
group => group.Select(pair => pair.Key));