使用MSDN文章"How to: Perform Left Outer Joins (C# Programming Guide)"上找到的技術,我嘗試在我的Linq代碼中創建左外連接。該文章提到使用DefaultIfEmpty
方法爲了從一個組連接創建一個左外連接。基本上,它指示程序包含左側(第一個)集合的結果,即使在正確的集合中沒有結果。Linq左外連接不工作
然而,這個程序執行的方式是這樣做的,就好像外部聯接沒有被指定一樣。
在我們的數據庫中,AgentProductTraining
是我們的代理商採取的一系列課程。通常情況下,您不能在CourseMaterials
表中輸入Course
到相應的表格中,而無需在表格中輸入相應的值。然而,偶爾會發生這種情況,所以我們希望確保我們能夠返回結果,即使在AgentProductTraining
中列出,但CourseMaterials
中沒有任何相應的信息。
var training = from a in db.AgentProductTraining
join m in db.CourseMaterials on a.CourseCode equals m.CourseCode into apm
where
a.SymNumber == id
from m in apm.DefaultIfEmpty()
where m.EffectiveDate <= a.DateTaken
&& ((m.TerminationDate > a.DateTaken) | (m.TerminationDate == null))
select new
{
a.AgentProdTrainId,
a.CourseCode,
a.Course.CourseDescription,
a.Course.Partner,
a.DateTaken,
a.DateExpired,
a.LastChangeOperator,
a.LastChangeDate,
a.ProductCode,
a.Product.ProductDescription,
m.MaterialId,
m.Description,
a.Method
};
有導航屬性'AgentProductTraining.CourseMaterials'? – 2013-02-19 20:58:07
此外,標題說_left內部聯接不working_,但我認爲這是外部聯接?事實上,你的文章中沒有真正的問題陳述。 – 2013-02-19 21:10:27
很好,趕快,謝謝。我不相信我們在'CourseMaterials'上有一個導航屬性 – NealR 2013-02-19 21:25:10