Linq to SQL包括相关表

时间:2017-12-11 10:02:21

标签: sql entity-framework linq

现在尝试了一段时间,但却无法按照我想要的方式工作。参考下面的代码片段,我想在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而弄乱了一些东西......

0 个答案:

没有答案