I need to output to the console the name of the group and the average score for each subject. But in Console.WriteLine Group and groupAverageMath are underlined with error "The name "Group"/"groupAverageMath" does not exist in the current context"
static void BestGroupAverage()
{
List<Student> students = ListManager.LoadSampleData();
var GroupAverageMath =
from student in students
group student by student.Group into studentGroup
select new
{
Group = studentGroup.Key,
groupAverageMath = studentGroup.Average(x => x.Math),
};
Console.WriteLine("\nGgroup with the best GPA in Math: " + Group + groupAverageMath);
Console.ReadLine();
}
Here's a very explicit solution. You didn't provide a definition of your
Studentclass, so I had to:Then, with very explicit steps so you can understand. Note that I provided data (because, again, you did not):
I get the groups. Then I get the maximum group average. Then I find out which group or groups got that average. Then I pick the first one.
This results in: