【发布时间】:2013-02-04 14:30:23
【问题描述】:
使用 MSDN 文章 "How to: Perform Left Outer Joins (C# Programming Guide)" 中的技术,我尝试在我的 Linq 代码中创建左外连接。文章提到使用DefaultIfEmpty 方法从组连接创建左外连接。基本上,它指示程序包含左(第一个)集合的结果,即使右集合中没有结果。
然而,这个程序的执行方式,就好像没有指定外连接一样。
在我们的数据库中,AgentProductTraining 是我们的代理所学课程的集合。通常,如果不将相应的值输入到CourseMaterials 表中,您就无法将Course 输入到相应的表中。但是,有时这种情况可能会发生,因此我们希望确保即使在 AgentProductTraining 中列出了 Course 而在 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? -
另外,标题说left inner join not working,但我认为它是outer join?实际上,您的帖子中并没有真正的问题陈述。
-
很好,谢谢。我不相信我们在
CourseMaterials上有导航属性
标签: asp.net-mvc asp.net-mvc-3 linq entity-framework-4