0

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();
}
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
  • The result of that query will be an `IEnumerable` (in variable `GroupAverageMath`) where `T` is an anonymously typed object with two properties: `Group` and `groupAverageMath`. It won't have any ordering. From that collection, you need to select the one with the highest `groupAverageMath`. You should see this in the debugger if you look at the `GroupAverageMath` variable – Flydog57 Dec 08 '21 at 16:59
  • You need to use the correct Uppercase/Lower case letters in the variable names. – jdweng Dec 08 '21 at 17:01
  • 1
    To amplify/refine @jdweng's comment: You don't actually _need_ to use the _correct_ naming to get this to work. But, you should really have some naming convention. There are two standard .NET/C# naming conventions (one prefaces member fields with an underscore, the other doesn't). Having consistent naming make reading and maintaining code so much simpler. You have a local variable starting with an uppercase letter (`GroupAverageMath`) and a property (of basically the same name) starting with a lower case letter (`groupAverageMath`) it makes it hard to read – Flydog57 Dec 08 '21 at 17:22

1 Answers1

1

Here's a very explicit solution. You didn't provide a definition of your Student class, so I had to:

public class Student
{
    public string Name { get; set; }
    public string Group { get; set; }
    public decimal Math { get; set; }
    public Student (string name, string group, decimal mathGrade)
    {
        Name = name;
        Group = group;
        Math = mathGrade;
    }
}

Then, with very explicit steps so you can understand. Note that I provided data (because, again, you did not):

public static void Test()
{
    //this is what you need to do in a question, provide
    //enough data so others can reproduce your issue
    var students = new List<Student>
    {
        new Student("A", "blue", 42),
        new Student("B", "blue", 88.5m),
        new Student("C", "green", 99m),
        new Student("D", "blue", 78.5m),
        new Student("E", "red", 66.6m),
        new Student("X", "red", 90),
        new Student("Y", "green", 28),
        new Student("Z", "blue", 80),
    };

    var groupAverageMath =
        from student in students
        group student by student.Group into studentGroup
        select new
        {
            Group = studentGroup.Key,
            GroupAverageMath = studentGroup.Average(x => x.Math),
        };
    var bestMathGrade = groupAverageMath.Max(gr => gr.GroupAverageMath);
    var bestGroups = groupAverageMath.Where(g => g.GroupAverageMath ==bestMathGrade);
    var bestGroup = bestGroups.FirstOrDefault();
    Console.WriteLine($"\nGroup with the best GPA in Math: {bestGroup.Group} score: {bestGroup.GroupAverageMath}");
    Console.ReadLine();
}

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:

Group with the best GPA in Math: red score: 78.3
Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • And how to implement a list of students so that it can be used for different tasks in the code - where each task is framed as a separate class method? (for example: Determine the student / students with the highest GPA; Calculate the average score for each subject; For each subject, determine the group with the best average score) – Anastay Kaiser Dec 09 '21 at 05:11
  • Your code showed (literally): `Ggroup with the best GPA in Math`. I showed you how to do it. Your code calls `ListManager.LoadSampleData()`. I assumed you had that code. Your question was purely about the best group in Math. That question is answered. You can't change the specs and complain the previously delivered solution doesn't meet the new specs. The answer I showed was deliberately very explanatory. There should be enough there for you to figure out your new question – Flydog57 Dec 09 '21 at 06:01
  • Your answer correct. Just wanna more details in other situations. But I already figure out this. So, thanks for helping – Anastay Kaiser Dec 09 '21 at 11:24