我使用 EF Core 8 为我的数据库建模。我想获取用户可以访问的租户列表。因此我使用以下查询:
var x = dbContext.Users
.Where(u => u.IdentityId == "089ebd4e-8fe0-48ce-93cd-c8c9ead5de9f")
.Include(u => u.Users2Groups)
.ThenInclude(u2G => u2G.Group)
.ThenInclude(g => g.Tenants2Groups)
.ThenInclude(t2G => t2G.Tenant)
.Distinct();
我尝试过使用
var y = x.Select(u => u.Users2Groups.Select(u2G => u2G.Group.Tenants2Groups.Select(t2G => t2G.Tenant))).ToList();
但这并没有给我一份租户名单。我如何获得租户名单?
应通过
SelectMany
选择。当查询中有多个表时,我更喜欢使用查询语法。
var query =
from u in dbContext.Users
where u.IdentityId == "089ebd4e-8fe0-48ce-93cd-c8c9ead5de9f"
from u2g in u.Users2Groups
from t2g in u2g.Group.Tenants2Groups
select t2g.Tenent;
query = query.Distinct();