在 LINQ 查询中连接列表

发布于 2024-11-03 01:36:53 字数 607 浏览 1 评论 0原文

我的数据结构是这样设置的

  • 一个用户使用多个模块
  • 一个模块包含多个课程

关系如下: 在此处输入图像描述

如何获取用户参加的课程列表?

我现在的查询是:

  var courses = (from ClassEnrollment enrolment in entities.ClassEnrollment
                       where enrolment.UserID == UserID
                       join Module module in entities.Module
                         on enrolment.ModuleID equals module.ID

                         select module.Course
                     ).ToList();

但是,这不会产生课程列表,而是课程列表的列表。

如何将此查询扁平化为不同课程的列表?

My data structure is set up this way

  • A user takes a number of modules
  • A module contains a number of courses

Here's how the relationship looks like:
enter image description here

How do I get a list of courses the user takes?

The query I have now is:

  var courses = (from ClassEnrollment enrolment in entities.ClassEnrollment
                       where enrolment.UserID == UserID
                       join Module module in entities.Module
                         on enrolment.ModuleID equals module.ID

                         select module.Course
                     ).ToList();

However, this doesn't result in a list of courses, but rather a list of list of courses.

How can I flatten this query to a list of distinct courses?

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

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

发布评论

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

评论(4

酷炫老祖宗 2024-11-10 01:36:53

根据您的数据结构屏幕截图,ClassEnrollmentModule 之间存在一对多关系,以及名为 Module 的导航属性。 ModuleCourse 之间还存在多对多关系,但导航属性应称为 Courses。给定您的代码,您想要这样的内容:

var courses = entities.
              ClassEnrollment.
              Where(e => e.UserID == UserID).
              SelectMany(e => e.Module.Courses).
              ToList();

但是,您的问题提到了一个用户:一个用户学习了多个模块如何获取用户学习的课程列表?< /代码>。不过,我在其他地方没有看到任何 User 实体,所以如果您能澄清一下就好了。顺便说一句,您正在使用 LINQ-to-SQL 吗?

According to your data structure screenshot, you have a one-to-many relationship between the ClassEnrollment and Module, as well as navigational property called Module. You also have a many-to-many relationship between Module and Course, but the navigational property should be called Courses. Given your code, you want something like this:

var courses = entities.
              ClassEnrollment.
              Where(e => e.UserID == UserID).
              SelectMany(e => e.Module.Courses).
              ToList();

Your question, however, mentions a user: A user takes a number of modules, How do I get a list of courses the user takes?. I don't see any User entity anywhere else, though, so it would be nice if you could clarify. Are you using LINQ-to-SQL, btw?

ら栖息 2024-11-10 01:36:53

像这样的事情:

var courses = from ClassEnrollment enrolment in entities.ClassEnrollment
              from module in entities.Module
              where enrolment.ModuleID equals module.ID && enrolment.UserID equals UserID
              select module.Course

Something like this:

var courses = from ClassEnrollment enrolment in entities.ClassEnrollment
              from module in entities.Module
              where enrolment.ModuleID equals module.ID && enrolment.UserID equals UserID
              select module.Course
北凤男飞 2024-11-10 01:36:53

您可以使用

course.SelectMany(c => c);

在查询中,您不需要显式指定范围变量的类型

,或者您可以将课程加入查询

var query = from enrolment in entities.ClassEnrollment
            join module in entities.Module on enrolment.ModuleID equals module.ID
            join course in entities.Course on module.CourseID equals course.ID
            where enrolment.UserID == UserID
            select course;

var course = query.ToList();

You can use

courses.SelectMany(c => c);

In your query you don't need explicitly specify the type for the range variables

Or you can join course to the query

var query = from enrolment in entities.ClassEnrollment
            join module in entities.Module on enrolment.ModuleID equals module.ID
            join course in entities.Course on module.CourseID equals course.ID
            where enrolment.UserID == UserID
            select course;

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