0

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 ?

Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
Developerzzz
  • 1,123
  • 1
  • 11
  • 26

1 Answers1

1

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; }
}
Jignesh Variya
  • 1,869
  • 16
  • 12