如何从此数据库架构中获取学生列表?

发布于 2024-10-31 00:46:21 字数 1055 浏览 1 评论 0原文

我有以下数据库:

学生
StudentID
姓名
姓氏 成绩
GradeId
姓名
GradeInstance
GradeInstanceId
GradeId
姓名
StudentInstance
StudentInstanceId
GradeInstanceId
StudentInstanceId
如何

从给定成绩实例中检索学生列表?

我使用实体框架作为 ORM,那么 LINQ 查询会是什么样子?

编辑:我需要返回 Student 对象,而不是 StudentInstance 对象,因为它们不包含我需要填充 GUI 的属性。

所以这样的事情对我不起作用,除非我在这里遗漏了一些东西。

dbContext.StudentInstances.Where(s => s.GradeInstanceId == 1);

I have the following database:

Student
StudentID
Name
LastName
Grade
GradeId
Name
GradeInstance
GradeInstanceId
GradeId
Name
Year
StudentInstance
StudentInstanceId
GradeInstanceId
StudentInstanceId

How can I retrieve a list of students from a given grade instance?

I'm using Entity Framework as my ORM so what would the LINQ query be like?

Edit: I need to return Student objects, not StudentInstance object because those don't contain the attributes I need to fill the GUI with.

So something like this doesn't work for me, unless I'm missing something here.

dbContext.StudentInstances.Where(s => s.GradeInstanceId == 1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我为君王 2024-11-07 00:46:21
from s in dbContext.Student
join si in dbContext.StudentInst on s.StudentID equals si.StudentInstanceID
join g in dbContext.Grade on si.GradeInstanceID equals g.GradeID
where g.Name = ...
select s;
from s in dbContext.Student
join si in dbContext.StudentInst on s.StudentID equals si.StudentInstanceID
join g in dbContext.Grade on si.GradeInstanceID equals g.GradeID
where g.Name = ...
select s;
悍妇囚夫 2024-11-07 00:46:21

这应该可以帮助您开始......

    public static List<Student> GetStudents(int gradeId)
    {
        using (var context = new Entities())
        {
            List<Student> myList = (from s in dbContext.Student 
join si in dbContext.StudentInst on s.StudentID equals si.StudentInstanceID
join g in dbContext.Grade on si.GradeInstanceID equals g.GradeID
where g.GradeId = gradeId
select s).ToList();

         return myList;
        }
    }

使用稍微修改过的 查询 :D

This should help you get started....

    public static List<Student> GetStudents(int gradeId)
    {
        using (var context = new Entities())
        {
            List<Student> myList = (from s in dbContext.Student 
join si in dbContext.StudentInst on s.StudentID equals si.StudentInstanceID
join g in dbContext.Grade on si.GradeInstanceID equals g.GradeID
where g.GradeId = gradeId
select s).ToList();

         return myList;
        }
    }

Using slightly modified query :D

静水深流 2024-11-07 00:46:21

您可以将导航属性添加到 GradeInstance 实体到与其相关的 Student 实体集合(您实际上可以从“添加关联”向导中执行此操作),然后您可以简单地通过以下方式访问它: gradeInstance.Students< /code>

希望这有帮助

you could add a navigation property to your GradeInstance Entity to the collection of Student entities related to it (you actually have the possibility to do that from the Add Association wizard), then you could acess it simply with: gradeInstance.Students

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文