假设我在 1 到 1 或 0 关系上有 2 个表,如下所示:
Invoice: ID, Date, OrderID
Order: ID, Date, Notes (string)
就 LINQtoSQL 而言,我的 INNER JOIN 为:
customer
.invoices
.OrderByDescending(i => i.Date)
.Join(context.Orders,
invoice => invoice.OrderID,
order => order.OrderID,
(invoice, order) => new
{
invoice = invoice,
OrderNotes = order.Notes
})
.ToList()
在内连接中排除的所有值都是WHERE Invoice.OrderID NOT IN (SELECT OrderID FROM Order)
,我没有检查会发生什么WHERE Invoice.OrderID IS NULL
所需的查询是:
SELECT i.*, o.Notes AS OrderNotes
FROM
Invoice AS i
LEFT OUTER JOIN
Order AS o
ON i.OrderID = o.OrderID
我知道应该切换 fk,但我无权更改数据库的结构。
我不知道如何产生 LEFT OUTER JOIN 效果。环顾四周,我只找到了 1 到多个 LEFT JOIN 的解决方案。