LINQ连接语句问题

时间:2013-08-01 07:44:26

标签: c# linq

我遇到LINQ语句问题。

我有3个表:ExaminationsExaminationProtocolsSampleTests

到目前为止,我已经使用了这个语句,因为我只需要前两个表的信息。

var baseQuery = from e in context.Examinations
                join ep in context.ExaminationProtocols
                on e.ID equals ep.ExaminationID into er
                from r in er.DefaultIfEmpty()
                select new { E = e, EP = r };

但现在我需要获得至少有ExaminationProtocols SampleTest字段为acccurate = true的{​​{1}}。

SampleTestExaminationProtocols之间的外键是

EP.ID equal ST.examinationProtocolID

我试图加入声明中的第三个表,但似乎没有得到我想要的结果。

如果有人能给我一个提示,我会很感激。

2 个答案:

答案 0 :(得分:2)

这能否为您提供所需的结果?

var baseQuery = from e in context.Examinations
                join ep in context.ExaminationProtocols
                    on e.ID equals ep.ExaminationID into er
                from r in er.DefaultIfEmpty()
                join st in context.SampleTests
                    on r.ID equals st.examinationProtocolID
                where st.acccurate
                select new { E = e, EP = r };

答案 1 :(得分:0)

可以像这样写出

var baseQuery = from e in Examinations
              join ep in ExaminationProtocols.Where(x => SampleTests.Where(y => y.accurate).Select(z => z.examinationProtocolID).Contains(x.ID))
                  on e.ID equals ep.ExaminationID into er
              from r in er.DefaultIfEmpty()
              select new { E = e, EP = r };