在linq-to-entities中,如何使用已过滤的子项列表返回对象?

时间:2010-05-12 19:44:50

标签: c#-3.0 linq-to-entities

我有以下型号

EDMX model representing a ProductionBlock with 0 or many ProductionLog

第一步是选择所有ProductionBlock

var blocks = context.ProductionBlocks;

如何将没有结束时间的ProductionLogProductionBlock合并?

我试图使用像

这样的反向查找来做到这一点
var blocks = context
    .ProductionLogs
    .Include("FK_ProductionLog_ProductionBlock")
    .Where(log => log.EndTime == null).Select(log => log.ProductionBlock)
    .Union(context.ProductionBlocks);

但这些块不包含任何ProductionLogs。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

如果我不在基地,请告诉我,但你想要。

 var logs = (
       from pl in context.ProductionLogs.Include("ProductionBlock")
            where pl.EndTime == null
       select pl);

然后,您将拥有一个日志和块列表。

var blocks  = logs.SelectMany(x=>x.ProductionBlock)
相关问题