这是我正在使用的一组简化的表,描述为类。我正在使用 T4 模板来创建简单的 POCO。 我已经删除了所有可能混淆问题的类的所有非必要属性。
public class MarketingPlan
{
public guid MarketingPlanID { get; set; }
public bool Disabled { get; set; }
public virtual ICollection<MarketingPlanItem> MarketingPlanItems { get; set; }
}
public partial class MarketingPlanItem
{
public System.Guid MarketingPlanItemID { get; set; }
public System.Guid MarketingPlanID { get; set; }
public System.Guid MarketingPlanItemTypeID { get; set; }
public bool Disabled { get; set; }
public Nullable<System.Guid> EmailTemplateID { get; set; }
public virtual EmailTemplate EmailTemplate { get; set; }
public virtual MarketingPlanItemType MarketingPlanItemType { get; set; }
}
public partial class EmailTemplate
{
public System.Guid EmailTemplateID { get; set; }
}
public partial class MarketingPlanItemType
{
public System.Guid MarketingPlanItemTypeID { get; set; }
}
我尝试创建的强类型结果不需要连接到实体框架上下文。这是我对解决方案的尝试。
public MarketingPlan GetMarketingPlanWithItems(Guid marketingPlanID)
{
var query =
this.Context
.MarketingPlanItems
.GroupJoin(this.Context.MarketingPlanItemTypes,
mpi => mpi.MarketingPlanItemTypeID,
mpit => mpit.MarketingPlanItemTypeID,
(mpi, mpit) =>
{
mpi.MarketingPlanItemType = mpit.FirstOrDefault();
return mpi;
})
.GroupJoin(this.Context.EmailTemplates,
mpi => mpi.EmailTemplateID,
et => et.EmailTemplateID,
(mpi, et) =>
{
mpi.EmailTemplate = et.FirstOrDefault();
return mpi;
})
.Where(mpi => mpi.Disabled == false);
var result =
this.Context
.MarketingPlans
.GroupJoin(query,
mp => mp.MarketingPlanID,
mpi => mpi.MarketingPlanID,
(mp, mpi) =>
{
mp.MarketingPlanItems = mpi.ToList();
return mp;
})
.Where(mp => mp.MarketingPlanID == marketingPlanID)
.FirstOrDefault();
return result;
}
现在我意识到我不能在中使用真正的匿名函数,GroupJoin
因为它会引发以下错误:
带有语句体的 lambda 表达式不能转换为表达式树
在这个例子中我是这样编码的,因为如果我new
是一个强类型的对象,我相信我必须填充我不想做的每个字段。
最终的结果是拥有;a仅MarketingPlan
填充MarketingPlanItems
了那些不是的Disable
,它EmailTemplate
由 EmailTemplate 或 Null 填充,并且每个 MarketingPlanItem 都填充了它MarketingPlanItemType
。用于此的 sql 可能看起来像(大致):
SELECT
mp.*,
mpi.*,
mpit.*,
et.*
FROM
MarketingPlan mp
LEFT JOIN MarketingPlanItem mpi
on mp.MarketingPlanID = mpi.MarketingPlanID
INNER JOIN MarketingPlanItemType mpit
on mpi.MarketingPlanItemTypeID = mpit.MarketingPlanItemTypeID
LEFT JOIN EmailTemplate et
on mpi.EmailTemplateID = et.EmailTemplateID
有没有办法在 Entity Framework 中使用 Lambda 完成此操作,而无需对数据库执行多个请求?
更新 1
public MarketingPlan GetMarketingPlanWithItems(Guid marketingPlanID)
{
MarketingPlan result = null;
var query = this.Context.MarketingPlanItems
.Include("MarketingPlan")
.Include("MarketingPlanItemType")
.Include("EmailTemplate")
.Include("EmailTemplate.EmailTemplateCategory")
.Where(mp => !mp.Disabled
&& !mp.MarketingPlan.Disabled
&& mp.MarketingPlanID == marketingPlanID)
.ToList();
var query2 = query.FirstOrDefault();
if (query2 != null)
{
result = query2.MarketingPlan;
result.MarketingPlanItems = query;
}
return result;
}
这最终返回了我需要的东西。