Linq2Sql和LEFT OUTER JOIN

时间:2013-12-04 11:45:34

标签: linq-to-sql

我有那个表架构

enter image description here

通过linq2Sql,我生成一个查询:

var types = (from d in TabD
             where d.SomeID == myID
             group d by d.TabC.TabB.TabA into g
             select g.Key);

生成该查询

SELECT Distinct1.ID, Distinct1.Name
FROM (
        SELECT DISTINCT ss.ID, ss.Name
        FROM TabD AS d
        INNER JOIN TabC AS c ON d.TabC_ID = c.ID
        INNER JOIN TabB AS b ON c.TabB_ID = b.ID
        LEFT OUTER JOIN TabA AS a ON b.TabA_ID = a.ID
        WHERE sg.SomeID = 1
     ) AS Distinct1

哪有效,但为什么有LEFT OUTER JOIN而不是INNER JOIN

1 个答案:

答案 0 :(得分:0)

只要表上的外键可以为空,就会获得LEFT JOIN。如果您的数据库将TabA_ID定义为TabA_ID int NULL,则表示未强制执行TabB对TabA的引用。

由于它没有根据您的架构强制执行,LINQ2SQL会生成一个LEFT连接。

希望这有帮助