Linq Dynamic query and include non related entity

c# dynamic-linq entity-framework linq linq-expressions

Question

I have a scenario where I have 3 Entities

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 and entityB have relation but entityC has not relation with any of them.

To get entityA and related entityB data I can do

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

but I can't do include() with entityA and entityC because there is no relationship between entityA and entityC.

It is only possible with Linq Query syntax like bellow:

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

Is there is any way to include() or join entityA and entityC using Linq Lambda syntax ?

1
0
4/10/2017 6:05:46 AM

Popular Answer

According to your query there is relationship between A and C logically. You can put ForeignKey attribute in C class entity mapping and then you can use Include in query.

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

  public Guid HistoryId { get; set; }

  [ForeignKey("HistoryId")]
  public entityA EntityA { get; set; }
}
0
4/10/2017 5:48:04 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow