现在尝试了一段时间,但却无法按照我想要的方式工作。参考下面的代码片段,我想在Bricks(BrickColors)中包含一个相关的表。截至目前,BrickColors不包含在内,并且已经延迟加载。
var query = (from ul in DbContext.UserLocs
join l in DbContext.Locs on ul.LocId equals l.Id
join lb in DbContext.LocBricks on l.Id equals lb.LocId
join b in DbContext.Bricks on lb.BrickId equals b.Id
join bc in DbContext.BrickColors on b.ColorId equals bc.Id
where ul.UserId == userId
group new { LocQty = ul.Quantity, LocBrickQty = lb.Quantity, Brick = b } by new { b.BrickId, b.ColorId }
into data
orderby data.Key
select new
{
Brick = data.FirstOrDefault().Brick,
Quantity = data.Sum(d => d.LocBrickQty * d.LocQty)
})
.AsNoTracking();
如果我删除.AsNoTracking(),性能非常好,因为它将BrickColors表保留在内存中,但我希望它从一开始就包含在查询中。
我在查询中尝试了DbContext.Bricks.Include(b => b.BrickColorAccessor),但这不起作用。我认为我的小组新{}因为我不在那里包含BrickColors而弄乱了一些东西......