如何在Select中填充多行实体框架

问题描述 投票:0回答:1

如何填写具有此类请求的公司列表? include 方法在我的实现中不起作用

   var list = await (from d in context.User.Where(x => ids.Contains(x.UserId))
                          from c in context.Company.Where(x => x.UserId == d.UserId)
                          select new User()
                          {
                             UserId = d.UserId
                             Companies = c.Select(x => new Company() {}) // - need Lists
                          }).ToListAsync(token);
c# entity-framework entity-framework-core
1个回答
0
投票

如果你想获取用户及其公司,我建议你尝试这个语句

var user = await context.User
  .Where(user => user.Id == userId)
  .Include(user => user.Companies)
  .ToListAsync(token);

var companies = user.Companies;

我认为这里使用 LINQ 是查询数据库的更好方法。

© www.soinside.com 2019 - 2024. All rights reserved.