LINQ嵌套查询

时间:2009-05-12 12:55:28

标签: linq-to-sql

如何使用LINQ编写以下SQL查询

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
     )

1 个答案:

答案 0 :(得分:3)

试试这个(为了避免包装而格式化奇怪...)

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;

但是,我很想把它作为一个连接来代替:

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;