【发布时间】:2016-11-29 09:16:25
【问题描述】:
是否可以在 Entity Framework Core(或 7)中从一个表中加载在另一个表中没有相关记录的所有记录?
在我的例子中,我有一个客户表和一个合同表。客户可以有 0 到 N 个合同。在这个特定的用例中,我想查询所有没有合同的客户。
**Customer Table:**
CustomerId | Name
---------- | ----------
1 | Apple
2 | Google
3 | Microsoft
**Contracts Table:**
| ContractId | CustomerId | StartDate | EndDate |
| ---------: | ---------: | ---------- | ---------- |
| 1 | 2 | 01-01-2016 | 01-01-2018 |
| 2 | 3 | 01-01-2016 | 01-01-2018 |
在这种情况下,我希望查询只返回一个包含 Apple 客户的对象。
在 SQL 中,我会这样做:
select cust.CustomerId, cust.Name
from dbo.Customers as cust
left outer join dbo.Contracts as contr
on cust.CustomerId = contr.CustomerId
where contr.ContractId is null;
如何将其转换为 EF 查询?
【问题讨论】:
标签: c# sql-server entity-framework