views:

57

answers:

2

I'm trying to do a left join on a constant like this:

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

I get a CS1941 when I attempt it in LINQ like this:

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()
+2  A: 

Try from Maintenances on ActionId == 8 first, then use this to join against.

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;
tvanfosson
In this case, the = is correct because I'm assigning a value to a property in an anonymous type.Your recommendation works, though. Thanks!
Robert S.
Right. I've updated to reflect that.
tvanfosson
A: 

or within the query:

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;
brian
This will make the ActionID == 8 part of the where clause, which will filter out any devices that don't have an Maintenance record of type 8.
Robert S.