2017-04-09 46 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; } 
}