Writing a nested join with LINQ to Entities

后端 未结 1 435
无人共我
无人共我 2021-01-22 02:12

I\'m pretty sure there\'s an example of this somewhere on here, I just can\'t find it as I don\'t know the exact terms. So please, be kind.

The problem should be a simpl

相关标签:
1条回答
  • 2021-01-22 02:17

    You can simulate with manual joins what EF does when you have relationships defined. All you need is to use Group Joins and projections. Something like this:

    var result =
        (from a in db.A
         where a.Id == IDParameter
         join b in db.B on a.Id equals b.AId into Bs
         select new
         {
             a,
             Bs =
                (from b in Bs
                 join c in db.C on b.Id equals c.BId into Cs
                 select new
                 {
                     b,
                     Cs =
                        (from c in Cs
                         join d in db.D on c.Id equals d.CId into Ds
                         select new
                         {
                             c,
                             Ds = Ds.ToList()
                         }).ToList() 
                 }).ToList()
         }).ToList();
    
    0 讨论(0)
提交回复
热议问题