表1:学生
StudentID, StudentName
表2:课程
CourseID, CourseName
表3:组
GroupID, GroupName
表4:学生课程组
StudentID, CourseID, GroupID
我希望所有属于"xyz"课程的学生采用以下格式
Class MyStudent
string StudentName
String [] Groups
每个学生都可以是一个或多个组的成员,我需要在LINQ查询中填充"Class MyClass",以便它在每个对象中包含StudentName
和List of groups。
你能建议一个LINQ查询吗?
var query = from s in db.Student
join scg in db.StudentCourseGroup on s.StudentID equals scg.StudentID
join c in db.Course on scg.CourseID equals c.CourseID
join g in db.Group on scg.GroupID equals g.GroupID
where c.CourseName == "xyz"
select new { s, g } into x
group x by x.s into studentGroups
select new MyStudent {
StudentName = studentGroups.Key.StudentName,
Groups = studentGroups.Select(sg => sg.g.GroupName)
};