2014-02-25 97 views
0

我怎樣才能linq 3表我可以連接一個學生和學校。表是:Students, Depart, School.我怎麼能LINQ 3表

studentId(pk), departId(fk) departId(pk), schoolId(fk) schoolId(pk)

下面是連接兩個表

@foreach (var student in Model.students.Where(s => s.schoolId == item.schoolId)) 
+0

請,加型號和你的控制器。 –

回答

0

你想要什麼?你可以加入他們的行列......

var query = (from depart in Model.Depart 
      join school in Model.School on depart.departId equals school.departId 
      join student in Model.students on school.schoolId equals student.schoolId 
      where students.schoolId == item.schoolId 
      select new 
      { 
       depart, 
       school, 
       student 
      }); 

或...

0

如果您有關聯cdonfigured正確,而不是加入,你也可以使用關聯:

var query = from school in Model.Schools 
      from dept in school.Departments 
      from student in dept.Students 
      select new { student, dept, school};