gpt4 book ai didi

c# - 如何在 LINQ 中查询基于链接表的数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:21 25 4
gpt4 key购买 nike

表 1:学生

StudentID, StudentName

表 2:类(class)

CourseID, CourseName

表 3:

GroupID, GroupName

表 4:学生类(class)组

StudentID、CourseID、GroupID

我希望属于“xyz”类(class)的所有学生采用以下格式

Class MyStudent

string StudentName
String [] Groups

每个学生都可以是一个或多个组的成员,我需要在我的 LINQ 查询中填充“Class MyClass”,以便它在每个对象中包含 StudentName 和组列表。

能否请您推荐一个可以执行此操作的 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)
};

关于c# - 如何在 LINQ 中查询基于链接表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14155975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com