对3个表使用linq进行完全外连接
本文关键字:连接 linq 3个 | 更新日期: 2023-09-27 18:08:58
我在EntityModel中有3个实体:Customer, CustomerAddress(这是具有id和orderOnControl列的连接实体,不能在model中提交)和Address。我需要使linq查询FULL JOIN客户与地址。这个SQL查询是简单的:
select e1.[AddressID], e1.City, e3.CustomerID, e3.LastName from [SalesLT].[Address] as e1
full join [SalesLT].[CustomerAddress] as e2 on e1.[AddressID] = e2.[AddressID]
full join [SalesLT].[Customer] as e3 on e2.CustomerID = e3.CustomerID
但我需要用linq写这个,我找到了答案,那里有2个具有多对多关系的实体,但找不到任何连接实体,我会感激任何提示
试试吧。这个LINQ语句将通过连接表CustomerAddress把Customers和Addresses连接起来。
var query = from ca in context.CustomerAddress
join a in context.Address on ca.AddressId equals a.AddressId
join c in context.Customer on ca.CustomerId equals c.CustomerId
select new { a.AddressId, a.City, c.CustomerId, c.LastName };