2010-05-12 52 views
1

我有以下型號在linq-to-entities中,如何使用篩選的子列表返回對象?

EDMX model representing a ProductionBlock with 0 or many ProductionLog

的第一步是選擇所有ProductionBlock

var blocks = context.ProductionBlocks; 

我如何可以結合一個沒有結束的時間與ProductionBlockProductionLog

我試圖做它用反向查找類似

var blocks = context 
    .ProductionLogs 
    .Include("FK_ProductionLog_ProductionBlock") 
    .Where(log => log.EndTime == null).Select(log => log.ProductionBlock) 
    .Union(context.ProductionBlocks); 

但塊不包含任何ProductionLogs。我怎樣才能做到這一點?

回答

2

讓我知道如果我脫離基地,但你會想。

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

然後,您將有一個日誌和塊的列表。

var blocks = logs.SelectMany(x=>x.ProductionBlock) 
+0

謝謝,我能夠從你的建議,建立並制定出一個解決方案'VAR塊= context.ProductionLogs.Include( 「ProductionBlock」)。其中(日誌=> log.EndTime == NULL)。 ToList()。選擇(日誌=> log.ProductionBlock).Union(context.ProductionBlocks); ToList()這是因爲我有一個Xml列和聯合不與一個Xml列一起工作。 – 2010-05-12 20:09:37