如何在实体框架查询中使用leftouter join
我有两个表一个是项目,另一个是库存可用我想获得所有项目,并从库存可用表中获取其数量,这取决于特定部门
答案 0 :(得分:2)
例如 LINQ查询
var query = (from p in dc.GetTable<Person>()
join pa in dc.GetTable<PersonAddress>() on p.Id equals pa.PersonId into tempAddresses
from addresses in tempAddresses.DefaultIfEmpty()
select new { p.FirstName, p.LastName, addresses.State });
SQL翻译
SELECT [t0].[FirstName], [t0].[LastName], [t1].[State] AS [State]
FROM [dbo].[Person] AS [t0]
LEFT OUTER JOIN [dbo].[PersonAddress] AS [t1] ON [t0].[Id] = [t1].[PersonID]