2017-04-09 91 views
0

我遇到的情況,我有3個實體Linq的動態查詢和包括非相關實體

class entityA 
{ 
    public Guid Id { get; set; } 
    public string NameA { get; set; } 
} 


class entityB 
{ 
    public Guid Id { get; set; } 
    public string NameB { get; set; } 
    public entityA EntityA { get; set; } 
} 

class entityC 
{ 
    public Guid Id { get; set; } 
    public Guid HistoryId { get; set; } 
} 

EntityA和entityB有關係,但entityC與任何人沒有關係。

要獲得entityA及相關entityB數據我可以做

db.entityA.Include(x=>x.entityB) 

,但我不能做包括()與entityA和entityC因爲entityA和entityC之間沒有任何關係。

它是唯一可能與像波紋管LINQ查詢語法:

from A in entityA join C in entityC on A.Id equals C.HistoryId select A 

有沒有什麼辦法可以包括()或者使用LINQ lambda語法加入entityA和entityC?

+0

選中此http://stackoverflow.com/a/2767742/2224701 –

+0

的可能的複製【如何做LINQ與方法語法SQL中加入?](http://stackoverflow.com/questions/3217669 /如何做-DO-A-加入在-LINQ到SQL的使用法,語法) –

回答

0

根據您的查詢,邏輯上A和C之間存在關係。 您可以將ForeignKey屬性放在C類實體映射中,然後您可以使用Include在查詢中。

class entityC 
{ 
    public Guid Id { get; set; } 

    public Guid HistoryId { get; set; } 

    [ForeignKey("HistoryId")] 
    public entityA EntityA { get; set; } 
}