C# 错误:“StudentScore”是一个类型,在给定上下文中无效
加入两个集合,按科目计算每个学生的总分C# 错误:“StudentScore”是一个类型,在给定上下文中无效,c#,linq,C#,Linq,加入两个集合,按科目计算每个学生的总分 public class Student { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } public class StudentScore { public int StudentId { get; set; }
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class StudentScore
{
public int StudentId { get; set; }
public string Subject { get; set; }
public int Points { get; set; }
}
public class StudentScoress
{
public static void Main(string[] args)
{
var totalscore = from s in Student
join ss in StudentScore
on s.Id equals ss.StudentId
group ss by ss.Subject into sss
select new { sss.Name, sss.Subject, sss.Points };
foreach (var ts in totalscore)
{
Console.WriteLine("{0}" + " " + "{1}" + " " + "{2}", ts.Name, ts.Subject, ts.Points);
}
}
IEnumerable<Student> student = new List<Student>()
{
new Student() {Id=1,Name="Sam",Age=16},
new Student() {Id=2,Name="Rick",Age=16},
new Student() {Id=3,Name="Warner",Age=17},
};
IEnumerable<StudentScore> studentScores = new List<StudentScore>()
{
new StudentScore() { StudentId = 1, Subject = "Maths", Points = 54},
new StudentScore() { StudentId = 1, Subject = "Maths", Points = 32},
new StudentScore() { StudentId = 1, Subject = "English", Points = 55},
new StudentScore() { StudentId = 1, Subject = "English", Points = 54},
new StudentScore() { StudentId = 1, Subject = "Biology", Points = 32},
new StudentScore() { StudentId = 1, Subject = "Biology", Points = 27},
new StudentScore() { StudentId = 2, Subject = "Maths", Points = 44},
new StudentScore() { StudentId = 2, Subject = "Maths", Points = 37},
new StudentScore() { StudentId = 2, Subject = "English", Points = 59},
new StudentScore() { StudentId = 2, Subject = "English", Points = 64},
new StudentScore() { StudentId = 2, Subject = "Biology", Points = 42},
new StudentScore() { StudentId = 2, Subject = "Biology", Points = 67},
new StudentScore() { StudentId = 3, Subject = "Maths", Points = 53},
new StudentScore() { StudentId = 3, Subject = "Maths", Points = 72},
new StudentScore() { StudentId = 3, Subject = "English", Points = 54},
new StudentScore() { StudentId = 3, Subject = "English", Points = 59},
new StudentScore() { StudentId = 3, Subject = "Biology", Points = 87},
new StudentScore() { StudentId = 3, Subject = "Biology", Points = 34}
};
}
我在LINQ查询中发现一个错误,即“StudentScore”是一个类型,在给定上下文中无效
我错过了什么。有人帮我吗
您应该用学生列表替换学生模型。
您应该用studentScores列表替换studentScores模型。
您需要通过两个字段对新的{s.Name,ss.Subject}进行分组,以便能够获得选择结果。
使用聚合运算符(如Sum函数)来获取TotalScore等。
输出
它必须来自s in student加入ss in studentScores-您必须引用您的变量,而不是查询中的类。@germi Thank..它提供了一个清晰的说明。非常感谢@Phong想问一些问题以供我澄清。1.如果我将IEnumerables放在main方法之外,如何调用student和studentScores变量。2.为什么我要在“分组依据”中添加“姓名”字段,因为我想根据主题分组依据。3.如何在输出中显示输出而不重复名称,如名称:Sam,科目总分:{“数学”:86,“英语”:109,“生物学”:59}1。为什么要将其置于方法之外?您可以创建其他方法,如GetListOfStudents等。之后,您可以调用该方法。在第二个问题中,我们应该尽量保持问题的简单性,以便将来帮助读者。这样你就可以提出另一个问题了?与相应的结果。把问题链接发给我。我愿意帮助你。当然可以@phong。。我将在即将发布的帖子中遵循您的建议。
var totalscore = from s in student
join ss in studentScores on s.Id equals ss.StudentId
group ss by new {s.Name, ss.Subject} into sss
select new { sss.Key.Name, sss.Key.Subject, TotalScore = sss.Sum(p => p.Points) };
foreach (var ts in totalscore)
{
Console.WriteLine("{0}" + " " + "{1}" + " " + "{2}", ts.Name, ts.Subject, ts.TotalScore);
}
Sam Maths 86
Sam English 109
Sam Biology 59
Rick Maths 81
Rick English 123
Rick Biology 109
Warner Maths 125
Warner English 113
Warner Biology 121