【问题标题】:Linq GroupJoin with multipe tablesLinq GroupJoin 与多个表
【发布时间】:2018-10-25 17:47:16
【问题描述】:

我通过 Linq 查询来获取用户并添加 UserClaimCollection。

public async Task<List<User>> GetCollection()
{
    List<User> result = _identityContext.Users
        .GroupJoin(
            _identityContext.UserClaims,
            u => u.Id,
            uc => uc.UserId,
            (u, uc) => new { user = u, claims = uc })
        .ToList()
        .Select(u => new User(
            new Guid(u.user.Id),
            u.user.UserName,
            u.user.FirstName,
            u.user.LastName,
            u.user.Email,
            new UserClaimCollection(
                u.claims.Select(uc => new UserClaim(uc.ClaimType, uc.ClaimValue)).ToList()
            ))
        ).ToList();

    return result;
}

如何向此选择添加第二个 GroupJoin 以添加角色集合?

例如

public async Task<List<User>> GetCollection()
{
    List<User> result = _identityContext.Users
        .GroupJoin(
            _identityContext.UserClaims,
            u => u.Id,
            uc => uc.UserId,
            (u, uc) => new { user = u, claims = uc })
        .GroupJoin(
            _identityContext.UserRoles,
            u => u.Id,
            ur => ur.UserId,
            (u, ur) => new { user = u, roles= ur })                    
        .ToList()
        .Select(u => new User(
            new Guid(u.user.Id),
            u.user.UserName,
            u.user.FirstName,
            u.user.LastName,
            u.user.Email,
            new UserClaimCollection(
                u.claims.Select(uc => new UserClaim(uc.ClaimType, uc.ClaimValue)).ToList()
            )),
            new UserRoleCollection(
                u.roles.Select(ur => new UserRole(ur.RoleType, ur.RoleValue)).ToList()
            ))
        ).ToList();

    return result;
}         

【问题讨论】:

  • 您应该在第二个GroupJoin 中有u =&gt; u.user.Id。记住第一个 GroupJoin 的输出是什么。你应该有包括所有的输出。

标签: .net linq .net-core


【解决方案1】:

您需要记住第一个GroupJoin 的输出是您的新匿名类,因此您就是传递给第二个GroupJoin 的内容。我建议命名 lambda 参数以提醒您这一点。

List<User> result = _identityContext.Users
    .GroupJoin(
        _identityContext.UserClaims,
        u => u.Id,
        c => c.UserId,
        (user, claims) => new { user, claims })
    .GroupJoin(
        _identityContext.UserRoles,
        uc => uc.user.Id,
        roles => roles.UserId,
        (uc, roles) => new { uc.user, uc.claims, roles })                    
    .ToList()
    .Select(ucr => new User(
        new Guid(ucr.user.Id),
        ucr.user.UserName,
        ucr.user.FirstName,
        ucr.user.LastName,
        ucr.user.Email,
        new UserClaimCollection(
            ucr.claims.Select(c => new UserClaim(c.ClaimType, c.ClaimValue)).ToList()
        )),
        new UserRoleCollection(
            ucr.roles.Select(r => new UserRole(r.RoleType, r.RoleValue)).ToList()
        ))
    ).ToList();

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多