2013-08-01 139 views
2

我有一個LINQ語句的問題。LINQ加入聲明問題

我有3個表格:Examinations,ExaminationProtocolsSampleTests

直到現在我已經使用了這個語句,因爲我只需要前兩個表的信息。

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 }; 

但現在我需要獲得至少有1 SampleTest有現場acccurate = trueExaminationProtocols

SampleTestExaminationProtocols之間的外鍵是

EP.ID equal ST.examinationProtocolID 

我想加入第三個表的語句中,但似乎沒有得到我想要的結果。

如果有人能給我一個提示,我會很感激。

回答

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 }; 
+0

謝謝你,它的工作太棒了! – Jordan

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 };