tags:

views:

37

answers:

1

How can i write following SQL query using LINQ

SELECT priority 
FROM Active_SLA 
WHERE APP_ID = (
        SELECT APP_ID 
        FROM FORM_PAGES 
        WHERE PAGE_ADDRESS = @address
    ) 
AND PERSON_ID = (
        SELECT PERSON_ID 
        FROM PERSON_DEVICES 
        WHERE DEVICE_NUMBER = @devicenum
     )
+3  A: 

Try this (formatting oddness for the sake of avoiding wrapping...)

var query = from sla in db.ActiveSLA
            where sla.AppId == (db.FormPages
                         .Where(page => page.PageAddress == address)
                         .Single().AppId)
               && sla.PersonId == (db.PersonDevices
                         .Where(pd => pd.DeviceNumber == deviceNumber)
                         .Single().PersonId)
            select sla.Priority;

However, I'd be very tempted to do this as a join instead:

var query = from sla in db.ActiveSLA
            join fp in db.FormPages on sla.AppId equals fp.AppId
            where fp.PageAddress == address
            join pd in db.PersonDevices on sla.PersonId equals pd.PersonId
            where pd.DeviceNumber == deviceNumber
            select sla.priority;
Jon Skeet