EF Core - 我们可以在包含的项目列表中调用 Include 吗?

EF Core - Can we call Include on a list of included items?

我有这个查询:

var orders1 = (from header in _context.SalesOrder.Include(nameof(SalesOrder.SalesOrderItem))
                                                    .Include(nameof(SalesOrder.Erp))
                                                    .Include(nameof(SalesOrder.OrderStatus))
                join orgNumber in _context.OrganizationNumber on header.CustomerNumber equals orgNumber.Number
                join userOrg in _context.UserOrganization on orgNumber.OrganizationId equals userOrg.OrganizationId
                where userOrg.UserId == userId && !header.Deleted && !orgNumber.Deleted && !userOrg.Deleted
                        && (header.OrderNumber == sopoNumber || header.CustomerPoNumber == sopoNumber)
                select GetSalesOrderModel(header)).AsNoTracking().ToList();

主tableSalesOrder,与SalesOrderItem是一对多的关系。 SalesOrderItemSalesOrder 一样,对 OrderStatus table 有外键。但我不确定如何将 Include 添加到已包含 SalesOrderItem 的列表中。是否可以直接在查询中添加它?当我填充模型对象时,我可以事后做,但想看看我是否可以一次性完成所有这些。

对每个子导航使用 ThenInclude() 属性。 参考:Loading Related Data - Including Multiple Levels

所以这是另一件与答案完全无关的事情,但即便如此,我还是将它发布在这里,供那些在 include 和 include 中苦苦挣扎的人

在 EFCore include(x=>x.Object).ThenInclude(y=>y.Subobject) 中也很好用但是 vs intellisence doset 把它捡起来

所以你可以使用 .Include(x=>x.SalesOrderItem)[=11= 而不是使用 .Include(nameof(SalesOrder.SalesOrderItem)) ]

回到你的问题

下面是示例代码

 vmExecutionRepo.FilteredGet().Include(x => x.ProjectToProcess).Include(z => z.ProjectToProcess.Process)
            .Include(y => y.ProjectToProcess.Project)
            .Include(a => a.ProjectToProcess.Project.BotToWorkQueue).ThenInclude(v => v.BotToWorkQueueItem)

你也可以尝试这样的东西它应该工作