Aggregate of aggregate with EF Core Linq2Sql

后端 未结 2 1619
南方客
南方客 2021-01-23 13:19

I have a ASP.NET Core 2.2 project with EF Core 2.2 Code-First DB. I have the following entities:

  • Building, which is basically an address with some other importan
2条回答
  •  时光取名叫无心
    2021-01-23 14:08

    What if you didn't use EF Navigations properties but used manual joins with LINQ to EF?

    var ans2 = (from b in dbContext.Buildings
                join f in dbContext.Floors on b.Id equals f.BuildingId into fj
                from f in fj.DefaultIfEmpty()
                join r in dbContext.Rooms on f.Id equals r.FloorId into rj
                from r in rj.DefaultIfEmpty()
                join ro in dbContext.RoomOccupancies on r.Id equals ro.RoomId
                join w in dbContext.WorkGroups on ro.WorkGroupId equals w.Id into wj
                from w in wj.DefaultIfEmpty()
                where !w.IsFinished && w.StartDate < DateTime.Now
                select new BuildingDatableElementDTO() {
                    BuildingId = b.Id,
                    Name = b.Name,
                    FloorCount = fj.Count(),
                    RoomCount = rj.Count(),
                    CurrentWorkerCount = wj.Sum(w => w.NumberOfEmployees)
               })
               .ToList();
    

提交回复
热议问题