Writing a nested join with LINQ to Entities

試著忘記壹切 提交于 2019-12-01 23:23:20

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!