用 lambda 表达式连接 3 个表?

发布于 2024-10-17 22:37:56 字数 246 浏览 0 评论 0原文

我基本上希望将以下 sql 查询作为 lambda 表达式:

SELECT studentname, coursename, grade
FROM student S, course C, grade G
WHERE S.id = G.studentid AND C.coursecode = G.coursecode AND G.grade<='B';

我遇到了麻烦,因为我必须将 3 个表连接在一起。

I basically want the following sql query as a lambda expression:

SELECT studentname, coursename, grade
FROM student S, course C, grade G
WHERE S.id = G.studentid AND C.coursecode = G.coursecode AND G.grade<='B';

I am having trouble as I have to join 3 tables together.

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

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

发布评论

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

评论(1

以歌曲疗慰 2024-10-24 22:37:56

好吧,作为查询表达式,它看起来像这样:(

var q = from grade in db.Grades
        where grade.grade <= 'B'
        join student in db.Students on grade.studentid equals student.studentid
        join course in db.Courses on grade.coursecode equals course.coursecode
        select new { student.studentname, course.coursename, grade.grade };

我通常使用 query 变量名而不是 q - 我刚刚使用了 q 此处用于格式化目的。)

可以将其转换为使用 lambda 表达式的显式 Join 调用,但我强烈建议您对复杂查询使用查询表达式像这样。

请注意,我更改了查询的顺序,以便尽可能简单有效地表达“where”子句。无论如何,SQL 查询规划器很可能都会对其进行优化,但对于 LINQ to Objects 之类的东西来说,这会有所帮助。

Well, that looks something like this as a query expression:

var q = from grade in db.Grades
        where grade.grade <= 'B'
        join student in db.Students on grade.studentid equals student.studentid
        join course in db.Courses on grade.coursecode equals course.coursecode
        select new { student.studentname, course.coursename, grade.grade };

(I'd normally use a variable name of query instead of q - I've just used q for formatting purposes here.)

You could translate this into explicit Join calls with lambda expressions, but I'd strongly advise you to use query expressions for complex queries like this.

Note that I've changed the order of the query to allow the "where" clause to be expressed as simply and efficiently as possible. In all likelihood a SQL query planner would optimize it anyway, but for something like LINQ to Objects this would help.

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