【问题标题】:Multiple Left Joins against Entity Framework针对实体框架的多个左连接
【发布时间】:2010-02-07 00:19:29
【问题描述】:
我一直在寻找,但找不到解决方案...
这是我的实体...
- ContentPage(有许多 ContentPageZones)
- ContentPageZone(只有一个内容)
- 内容
我想通过 ID 查询 ContentPage ,并且我希望它包含所有相关的 ContentPageZones,这些 ContentPageZones 处于活动状态,并且至少有一个处于活动状态的 Content (两者都具有 IsActive bool 属性)。如果没有可用的具有活动内容的 ContentPageZone,我仍然希望 ContentPage 具有空的 ContentPageZone 列表。
建议?
【问题讨论】:
标签:
c#
linq
entity-framework
【解决方案1】:
在你的问题中,你说:
ContentPageZone(只有一个内容)
这意味着 ContentPageZone -> Content 是 1..[0,1]。
然后你说:
...所有处于活动状态的相关 ContentPageZones 并且至少有一个处于活动状态的 Content...
这意味着 ContentPageZone -> Content 是 1..*。
我假设第一个是正确的,但第二个的查询会有所不同。
var q = from cp in Context.ContentPages
where cp.ID == someId
select new
{
ID = cp.ID,
Name = cp.Name,
ContentPageZones = from cpz in cp.ContentPageZones
where cpz.Content.IsActive
select new
{
ID = cpz.ID,
// etc.
}
};