LINQ:如何从 3 个不同的表中选择 3 个不同的列值?

发布于 2025-01-08 11:47:05 字数 546 浏览 0 评论 0原文

我有三个表 CorevalueSubjectTypeQuestion

我想选择 CoreValue.SnameSubjectType.CnameQuestion.QuestionText,我知道它如何与 SQL 配合使用,但不知道 LINQ 的任何帮助受到赞赏。

SQL 中类似这样的内容:

SELECT 
   CoreValue.Cname, 
   Question.Questiontext, 
   SubjectType.Sname 
FROM 
   Corevalue 
JOIN Question
   ON Corevalue.CID = question.QID 
JOIN SubjecType 
   ON Question.QID = SubjectType.SID;

我想这个是正确的,但我想要 LINQ :/

感谢提前致以

最诚挚的问候!

I have three tables Corevalue, SubjectType and Question.

I want to select CoreValue.Sname, SubjectType.Cname and Question.QuestionText, I know how it works with SQL but not LINQ any help would be appreciated.

Somethng like this in SQL:

SELECT 
   CoreValue.Cname, 
   Question.Questiontext, 
   SubjectType.Sname 
FROM 
   Corevalue 
JOIN Question
   ON Corevalue.CID = question.QID 
JOIN SubjecType 
   ON Question.QID = SubjectType.SID;

I quess this one is right, but I want to LINQ :/

Thanks on Advance

Best Regards!

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

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

发布评论

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

评论(1

凉宸 2025-01-15 11:47:05

您也只需要在 SQL 中表达联接:

var query = from core in db.Cores
            join question in db.Questions on core.CID equals question.QID
            join subject in db.Subjects on question.QID equals subject.SID
            select new {
                core.CoreName,
                question.QuestionText,
                subject.SubjectName
            };

当然,如果您已经映射了数据库关系,则可能不需要显式执行联接,而是更喜欢简单的属性 - 这完全取决于您如何使用 LINQ。

请注意,就 ID 名称而言,您的连接似乎有点奇怪 - 就好像您确实有三个实体都使用完全相同相同的 ID。我期待更多这样的事情:

var query = from core in db.Cores
            join question in db.Questions on core.ID equals question.CoreID
            join subject in db.Subjects on core.ID equals subject.CoreID
            select new {
                core.CoreName,
                question.QuestionText,
                subject.SubjectName
            };

You just need to express the join in SQL too:

var query = from core in db.Cores
            join question in db.Questions on core.CID equals question.QID
            join subject in db.Subjects on question.QID equals subject.SID
            select new {
                core.CoreName,
                question.QuestionText,
                subject.SubjectName
            };

Of course if you've mapped your database relations, you may not need to perform the join explicitly, preferring simple properties instead - it all depends on how you're using LINQ.

Note that your joins seem slightly odd, in terms of the names of the IDs - it's as if you've really got three entities all using exactly the same IDs. I'd expect something more like this:

var query = from core in db.Cores
            join question in db.Questions on core.ID equals question.CoreID
            join subject in db.Subjects on core.ID equals subject.CoreID
            select new {
                core.CoreName,
                question.QuestionText,
                subject.SubjectName
            };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文