输出到具有最佳 GPA 的控制台组(使用 LINQ,无循环)
Output to console group with the best GPA (using LINQ, without loops)
我需要向控制台输出小组名称和每个科目的平均分数。
但是在 Console.WriteLine Group 和 groupAverageMath 下划线的错误是“The name “Group” /“groupAverageMath”在当前上下文中不存在
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();
}
这是一个非常明确的解决方案。您没有提供 Student
class 的定义,所以我不得不:
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;
}
}
然后,用非常明确的步骤让你明白。请注意,我提供了数据(因为,同样,你没有):
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();
}
我得到了组。然后我得到最大的组平均值。然后我找出哪个或哪些组得到了这个平均值。那我选第一个。
这导致:
Group with the best GPA in Math: red score: 78.3
我需要向控制台输出小组名称和每个科目的平均分数。 但是在 Console.WriteLine Group 和 groupAverageMath 下划线的错误是“The name “Group” /“groupAverageMath”在当前上下文中不存在
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();
}
这是一个非常明确的解决方案。您没有提供 Student
class 的定义,所以我不得不:
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;
}
}
然后,用非常明确的步骤让你明白。请注意,我提供了数据(因为,同样,你没有):
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();
}
我得到了组。然后我得到最大的组平均值。然后我找出哪个或哪些组得到了这个平均值。那我选第一个。
这导致:
Group with the best GPA in Math: red score: 78.3