将sql转换为linq

时间:2012-05-11 15:32:31

标签: sql linq entities

我是Linq世界的新手,我无法弄清楚如何将以下sql转换为linq,因为数据库中的某些约束会影响添加我需要的一些字段:

select sp.SessionID,
    sp.SessionParticipationID,
    case when z.SessionParticipationID = sp.SessionParticipationID then 1 else 0 end as RecordedFL
from SessionParticipation sp
inner join (
    select sp1.SessionID,
        sp1.SessionParticipationID
    from Meeting.SessionParticipation sp1
    inner join RegistrantActivity ra on ra.SessionParticipationID = sp1.SessionParticipationID
    where ra.RegistrantID = 12345
) z on z.SessionID = sp.SessionID

实体是SessionParticipation和RegistrantActivity

1 个答案:

答案 0 :(得分:1)

应该是

from sp in SessionParticipations
join z in
(from sp1 in MeetingSessionParticipations
join ra in RegistrantActivities on sp1.SessionParticipationID equals 
                                   ra.SessionParticipationID
where ra.RegistrantID = 12345
select sp1)
on sp.SessionID equals z.SessionID
select new { 
             sp.SessionParticipationID,
             RecordedFL = z.SessionParticipationID == sp.SessionParticipationID
                          ? 1 : 0
           }

假设Meeting.SessionParticipation是与SessionParticipation不同的模式中的不同表,否则连接会使事情变得不必要复杂。 (尽管不同模式中的相同表名称令人困惑)。