0

I have and entity model as follows.

public class reg{
  public int id {get; set;}
  ...
  public ICollection<trans> trans{get; set;}
}
public class trans{
  public int id {get; set;}
  ...
  public modifiedTrans modifiedTrans{get; set;}
  public virtual reg reg {get; set;}
}
public class modifiedTrans {
  public int id {get; set;}
  ...
  public virtual trans trans {get; set;}
}

I need a linq expression which gives me the result filtered by reg id as

{
 id : x
 ...
 trans : {
  id : y1,
  ...,
  modifiedTrans : {
   id : z1
   ...
   }
  },
  {
   id : y2,
   ...,
   modifiedTrans : {
   id : z2
   ...
  }
 }
}

So far I ended up like this:

var query = from r in reg
        let t = r.trans
        let m = t.modifiedTrans 
        where (r => r.Id = 1)
        select new
                       {
                        r = r,
                        t = t,
                        m = m
                        }.FirstOrDefault();

which is not really I would like to have. Can anyone please help!!

Praveen Prasannan
  • 7,093
  • 10
  • 50
  • 70

1 Answers1

1

That is a tricky answer because you have a recursively nested entity. Depending on how often your objects are deeply nested and how many of your objects or what percentage of the modifications you are loading you could be better off lazily or eagerly loading (or a combination thereof) your nested objects.

Updated Sorry, I misread your classes.

var result=_db.regs
  .Include(r=>r.trans)
  .Include(r=>r.trans.Select(t=>t.modifiedTrans))
  .Where(r=>r.id==1);
Robert McKee
  • 21,305
  • 1
  • 43
  • 57