children 的计数 1-* children *-* Linq Entity Framework

Count of childrens 1-* children *-* Linq Entity Framework

我对 LINQ 和 EF 有点陌生,正试图获取以下数据库中每位 class 教授的每位教师的学生总数。我想输出教师姓名,后跟 class 姓名,然后是 class 中的学生人数。由于没有代表,我无法嵌入图像,抱歉。

我不确定如何执行此操作,因为当我尝试使用两个 from 语句时,我得到类型推断失败。当我尝试使用联接并在 Class.TeachersId 上加入 Teacher.id 时,我收到一条错误消息 "The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'"

Class和教师表的模型如下:

看起来像这样的东西应该有效:

foreach (var teacher in context.Teachers)
{
    Console.WriteLine("The teacher '{0}' is teaching the following classes:", 
        teacher.Name);

    foreach (var teacherClass in teacher.Classes)
    {
        Console.WriteLine(" - {0} ({1} students)", 
            teacherClass.Name, teacherClass.Students.Count);
    }
}