LINQ 查询中多个 from 选择器的含义是什么

发布于 2025-01-07 17:39:04 字数 417 浏览 4 评论 0原文

我已经在 LINQ 查询中看到过多次 from 子句,但还没有弄清楚它如何与 TSQL 语句相对应。

var r = from p in products
        from d in departments
        from c in something
        select p;

它有什么作用?

这是否会翻译成 SQL 为 SELECT * FROM 产品、部门等

此外,本例中的 select 应该始终是 SelectMany。我如何知道 select 实际上是 SelectMany

I've seen multiple from clauses in a LINQ query a few times now, but have not figured out how that corresponds to a TSQL statement.

var r = from p in products
        from d in departments
        from c in something
        select p;

What does that do?

Does that translate to SQL as SELECT * FROM products, departments, something?

Also, the select in this case is supposed to always be a SelectMany. how do I know when a select actually is a SelectMany?

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

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

发布评论

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

评论(3

站稳脚跟 2025-01-14 17:39:04

从概念上讲,这将转化为如下代码:

var r = products.SelectMany(p => departments, (p, d) = new { p, d })
                .SelectMany(z => something, (z, c) => new { z, c })
                .Select(zz => zz.z.p);

基本上,第一个之后的每个 from 都会添加对 SelectMany 的额外调用。实际上,在这种情况下,编译器会注意到在最后一个 from 子句之后只有一个 select,并且它实际上将转换为:

var r = products.SelectMany(p => departments, (p, d) = new { p, d })
                .SelectMany(z => something, (z, c) => z.p);

。 ..但我倾向于首先考虑“逻辑”形式。

请注意,在这种情况下,您尚未在 from 子句中使用之前的范围变量,但您可以:

var query = from person in people
            from friend in person.Friends
            select person.Name + " is friends with " + friend.Name;

That will translate into code conceptually like this:

var r = products.SelectMany(p => departments, (p, d) = new { p, d })
                .SelectMany(z => something, (z, c) => new { z, c })
                .Select(zz => zz.z.p);

Basically each from after the first one adds an extra call to SelectMany. Actually, in this case the compiler will notice that after the last from clause there's just a select, and it will actually translate to this:

var r = products.SelectMany(p => departments, (p, d) = new { p, d })
                .SelectMany(z => something, (z, c) => z.p);

... but I tend to think of the "logical" form first.

Note that in this case you haven't used the earlier range variables within the from clauses, but you can:

var query = from person in people
            from friend in person.Friends
            select person.Name + " is friends with " + friend.Name;
一个人的夜不怕黑 2025-01-14 17:39:04

它如何对应于 TSQL 语句?

这取决于您使用 from 子句的方式。如果您使用:

var query = from d in context.Departments
            from e in context.Employees
            select d;

您的查询将是CROSS JOIN,因为所使用的产品和部门之间没有关系。所以类似:

SELECT d.* FROM Departments AS d
CROSS JOIN Employees AS e

显然你应该避免交叉连接。如果您在第二个 from 中使用导航属性:

var query = from d in context.Departments
            from e in d.Employees
            select d;

您将使用 INNERLEFT JOIN (EF 将根据您的情况决定使用哪一个)关系中的多重性映射)。所以像这样:

SELECT d.* FROM Departments AS d
LEFT OUTER JOIN Employees AS e ON d.Id = e.DepartmentId

How that corresponds to a TSQL statement?

It depends on the way how you use your from clauses. If you use:

var query = from d in context.Departments
            from e in context.Employees
            select d;

Your query will be CROSS JOIN because there is no relation between product and department used. So something like:

SELECT d.* FROM Departments AS d
CROSS JOIN Employees AS e

Obviously you should avoid cross joins. If you instead use navigation property in second from:

var query = from d in context.Departments
            from e in d.Employees
            select d;

you will use either INNER or LEFT JOIN (EF will decide which one to use based on your multiplicity mapping in the relation). So something like:

SELECT d.* FROM Departments AS d
LEFT OUTER JOIN Employees AS e ON d.Id = e.DepartmentId
独守阴晴ぅ圆缺 2025-01-14 17:39:04

在这种情况下,使用 select 没有任何意义(语义上不正确,您必须使用 SelectMany)

In this case it doesn't make any sense to use select (it's not semantically correct, you must use SelectMany)

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