我正试图在这样的常量上进行左连接:
SELECT
[t0].[DeviceId],
[t0].[DeviceTypeId],
[t0].[UnitId],
[t0].[UnitNum],
[t0].[ManPhone],
[t0].[Status],
[t2].[MaintDate] AS [ServiceExpiration]
FROM [dbo].[Devices] AS [t0]
INNER JOIN [dbo].[CustomerDevices] AS [t1]
ON ([t0].[DeviceId]) = [t1].[DeviceId]
LEFT JOIN [dbo].[Maintenance] AS [t2]
ON ([t0].[DeviceId]) = [t2].[DeviceId]
AND 8 = [t2].ActionId
当我在LINQ中尝试这样的时候,我得到CS1941:
var devices = from d in db.Devices
join cd in db.CustomerDevices
on d.DeviceId equals cd.DeviceId
join serviceExpiration in db.Maintenances
on
new { d.DeviceId, ActionId = 8 } // CS1941
equals
new { serviceExpiration.DeviceId, ActionId = serviceExpiration.ActionId } into j1
from deviceServiceExpiration in j1.DefaultIfEmpty()
答案 0 :(得分:6)
首先尝试维护ActionId == 8
,然后使用此加入。
var expirations = from serviceExpiration in db.Maintenances
where serviceExpiration.ActionId == 8
select serviceExpiration;
var devices = from d in db.Devices
join cd in db.CustomerDevices
on d.DeviceId equals cd.DeviceId
join e in expirations
on d.DeviceId = e.DeviceId into j1
from deviceServiceExpiration in j1.DefaultIfEmpty()
select deviceServiceExpiration;
答案 1 :(得分:0)
试试这个
var devices = from d in db.Devices
join cd in db.CustomerDevices
on d.DeviceId equals cd.DeviceId
join serviceExpiration in db.Maintenances
on d.DeviceId equals serviceExpiration.DeviceId into j1
from deviceServiceExpiration in j1.Where(c=>c.ActionId ==8).DefaultIfEmpty()
答案 2 :(得分:0)
在这里参加晚会,但我遇到了同样的问题,并希望将来帮助任何人。 修复是加入子查询。
var devices = from d in db.Devices
join cd in db.CustomerDevices
on d.DeviceId equals cd.DeviceId
join serviceExpiration in (from x in db.Maintenances where x.ActionId == 8 select x)
on d.DeviceId equals serviceExpiration.DeviceId into j1
from deviceServiceExpiration in j1.DefaultIfEmpty()
希望这有助于某人。
答案 3 :(得分:-1)
或在查询中:
var devices = from d in db.Devices
join cd in db.CustomerDevices
on d.DeviceId equals cd.DeviceId
join serviceExpiration in db.Maintenances
on
d.DeviceId
equals
serviceExpiration.DeviceId into j1
from deviceServiceExpiration in j1.DefaultIfEmpty()
where serviceExpiration.ActionId == 8
select deviceServiceExpiration;