LINQ 左连接生成错误的 SQL 查询
LINQ left join generates wrong SQL query
我有这个 LINQ 查询:
from sol in context.SalesOrderLines
join mpso in context.MulticlientsoParentChildsoRelations on sol.orderid equals mpso.parentsalesorderid into lmpso
from mpso in lmpso.DefaultIfEmpty()
join cso in context.SalesOrders on mpso.childsalesorderid equals cso.id into lcso
from cso in lcso.DefaultIfEmpty()
join csol in context.SalesOrderLines on cso.id equals csol.orderid into lcsol
from csol in lcsol.DefaultIfEmpty()
join pll in context.PickingLine on (csol == null ? sol.id : csol.id) equals pll.salesorderlineid
join pl in context.PickingLists on pll.pickinglistid equals pl.id
join pfi in context.PfiSubmitterQuantities on pll.id equals pfi.pickinglineid into ppfi
from pfi in ppfi.DefaultIfEmpty()
where sol.orderid == palletid && (pfi == null ? pl.wholepallets == true : true)
select new Helper { Id = pfi.palletid ?? pll.palletid, BoolValue01 = pfi == null }
这是左连接的第六行
join csol in context.SalesOrderLines on cso.id equals csol.orderid into lcsol
from csol in lcsol.DefaultIfEmpty()
它会生成奇怪的 SQL 查询,这是我对 sql 执行 linq 时的样子(看第 7 行):
SELECT distinct
CASE WHEN (pfi."palletid" IS NULL) THEN (pll."palletid") ELSE (pfi."palletid") END AS "C1",
CASE WHEN (pfi."id" IS NULL) THEN (TRUE) ELSE (FALSE) END AS "C2"
FROM salesorderlines AS sol
LEFT OUTER JOIN multiclientso_parentchildsorelations AS mpso ON sol."orderid" = mpso."parentsalesorderid"
LEFT OUTER JOIN salesorders AS cso ON mpso."childsalesorderid" = cso."id"
LEFT OUTER JOIN salesorderlines AS csol ON cso."id" = csol."orderid" OR cso."id" IS NULL AND csol."orderid" IS NULL
INNER JOIN pickingline AS pll ON
CASE WHEN (csol."id" IS NOT NULL) THEN (csol."id") ELSE (sol."id") END = pll."salesorderlineid"
OR CASE WHEN (csol."id" IS NOT NULL) THEN (csol."id") ELSE (sol."id") END IS NULL AND pll."salesorderlineid" IS NULL
INNER JOIN pickinglist AS pl ON pll."pickinglistid" = pl."id"
LEFT OUTER JOIN pfi_submittedquantities AS pfi ON pll."id" = pfi."pickinglineid"
WHERE sol."orderid" = 24863039
AND CASE WHEN (pfi."id" IS NULL) THEN ( CASE WHEN (TRUE = pl."wholepallets") THEN (TRUE) WHEN (NOT (TRUE = pl."wholepallets" AND pl."wholepallets" IS NOT NULL)) THEN (FALSE) END ) ELSE (TRUE) END = TRUE
问题是我的 linq left join 如何从这个 LINQ 转换
join csol in context.SalesOrderLines on cso.id equals csol.orderid into lcsol
from csol in lcsol.DefaultIfEmpty()
到这个SQL
LEFT OUTER JOIN salesorderlines AS csol ON cso."id" = csol."orderid" OR cso."id" IS NULL AND csol."orderid" IS NULL
OR 部分从何而来?
感谢@ArcaArtem,我发现 SalesOrderLines table 有 orderid 可为 null 的 int 外键字段,在我的例子中它永远不会为 null,因此将其更改为简单的 int。现在它生成正确的 sql.
我有这个 LINQ 查询:
from sol in context.SalesOrderLines
join mpso in context.MulticlientsoParentChildsoRelations on sol.orderid equals mpso.parentsalesorderid into lmpso
from mpso in lmpso.DefaultIfEmpty()
join cso in context.SalesOrders on mpso.childsalesorderid equals cso.id into lcso
from cso in lcso.DefaultIfEmpty()
join csol in context.SalesOrderLines on cso.id equals csol.orderid into lcsol
from csol in lcsol.DefaultIfEmpty()
join pll in context.PickingLine on (csol == null ? sol.id : csol.id) equals pll.salesorderlineid
join pl in context.PickingLists on pll.pickinglistid equals pl.id
join pfi in context.PfiSubmitterQuantities on pll.id equals pfi.pickinglineid into ppfi
from pfi in ppfi.DefaultIfEmpty()
where sol.orderid == palletid && (pfi == null ? pl.wholepallets == true : true)
select new Helper { Id = pfi.palletid ?? pll.palletid, BoolValue01 = pfi == null }
这是左连接的第六行
join csol in context.SalesOrderLines on cso.id equals csol.orderid into lcsol
from csol in lcsol.DefaultIfEmpty()
它会生成奇怪的 SQL 查询,这是我对 sql 执行 linq 时的样子(看第 7 行):
SELECT distinct
CASE WHEN (pfi."palletid" IS NULL) THEN (pll."palletid") ELSE (pfi."palletid") END AS "C1",
CASE WHEN (pfi."id" IS NULL) THEN (TRUE) ELSE (FALSE) END AS "C2"
FROM salesorderlines AS sol
LEFT OUTER JOIN multiclientso_parentchildsorelations AS mpso ON sol."orderid" = mpso."parentsalesorderid"
LEFT OUTER JOIN salesorders AS cso ON mpso."childsalesorderid" = cso."id"
LEFT OUTER JOIN salesorderlines AS csol ON cso."id" = csol."orderid" OR cso."id" IS NULL AND csol."orderid" IS NULL
INNER JOIN pickingline AS pll ON
CASE WHEN (csol."id" IS NOT NULL) THEN (csol."id") ELSE (sol."id") END = pll."salesorderlineid"
OR CASE WHEN (csol."id" IS NOT NULL) THEN (csol."id") ELSE (sol."id") END IS NULL AND pll."salesorderlineid" IS NULL
INNER JOIN pickinglist AS pl ON pll."pickinglistid" = pl."id"
LEFT OUTER JOIN pfi_submittedquantities AS pfi ON pll."id" = pfi."pickinglineid"
WHERE sol."orderid" = 24863039
AND CASE WHEN (pfi."id" IS NULL) THEN ( CASE WHEN (TRUE = pl."wholepallets") THEN (TRUE) WHEN (NOT (TRUE = pl."wholepallets" AND pl."wholepallets" IS NOT NULL)) THEN (FALSE) END ) ELSE (TRUE) END = TRUE
问题是我的 linq left join 如何从这个 LINQ 转换
join csol in context.SalesOrderLines on cso.id equals csol.orderid into lcsol
from csol in lcsol.DefaultIfEmpty()
到这个SQL
LEFT OUTER JOIN salesorderlines AS csol ON cso."id" = csol."orderid" OR cso."id" IS NULL AND csol."orderid" IS NULL
OR 部分从何而来?
感谢@ArcaArtem,我发现 SalesOrderLines table 有 orderid 可为 null 的 int 外键字段,在我的例子中它永远不会为 null,因此将其更改为简单的 int。现在它生成正确的 sql.