簡體   English   中英

C# LINQ IdentityContext 多組加入

[英]C# LINQ IdentityContext Multi Group Join

我有這個 LINQ:

var users = _ctx.Users
    .AsEnumerable() // or "InvalidOperationException: This may indicate either a bug or a limitation in EF Core"
    .GroupJoin(_ctx.UserClaims, u => u.Id, c => c.UserId, (user, claims) => new { user, claims })
    .GroupJoin(_ctx.UserRoles, uc => uc.user.Id, r => r.UserId, (uc, roles) => new { uc.user, uc.claims, roles })
    .ToList()
    .Select(ucr => new UserDto
        {
            Id = ucr.user.Id,
            UserName = ucr.user.UserName,
            FirstName = ucr.user.FirstName,
            LastName = ucr.user.LastName,
            LastLogin = ucr.user.LastLogin,
            LockoutEnd = ucr.user.LockoutEnd,
            LockoutEnabled = ucr.user.LockoutEnabled,
            Roles = ucr.roles.Select(x => x.RoleId).Distinct().ToList(), // Here i would like the Role Name from the Roles Table by the UserRoles
            Scopes = ucr.claims.Select(x => x.ToClaim()).Where(x => x.Type.Equals(Shared.Scopes.Key)).Select(x => x.Value).ToList(),
            Claims = ucr.claims.Select(x => x.ToClaim()).Where(x => !x.Type.Equals(Shared.Scopes.Key)).ToList(),
            Enabled = ucr.user.Enabled
    }
).ToList();

這工作得很好,但我想從 UserRoles 獲取用戶的實際角色,而不僅僅是用戶到角色的映射

我嘗試添加一個額外的.GroupJoin(_ctx.Roles, ucr => ucr.roles.Id, r => r.Id, (ucr, r) => new { u, r }).GroupJoin(_ctx.Roles, ucr => ucr.roles.Id, r => r.Id, (ucr, r) => new { u, r })但因為roles是列出我收到錯誤:

'IEnumerable<IdentityUserRole>' 不包含 'Id' 的定義,並且沒有可訪問的擴展方法 'Id' 接受類型為 'IEnumerable<IdentityUserRole>' 的第一個參數

任何想法我也可以獲得實際的角色列表?

我正在使用除 ApplicationUser 之外的默認身份類

    public class ApplicationUser : IdentityUser
    {
        /// <summary>
        /// First Name
        /// </summary>
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        /// <summary>
        /// Last Name
        /// </summary>
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        /// <summary>
        /// Profile Picture
        /// </summary>
        public byte[] ProfilePicture { get; set; }

        /// <summary>
        /// Enabled
        /// </summary>
        public bool Enabled { get; set; }

        /// <summary>
        /// Last Login
        /// </summary>
        [Display(Name = "Last Login")]
        [DataType(DataType.DateTime)]
        [DisplayFormat(ApplyFormatInEditMode = true, NullDisplayText = "N/A", DataFormatString = "{0:yyyy-MM-dd HH:mm}")]
        public DateTime? LastLogin { get; set; } = null;

        /// <summary>
        /// Display Name
        /// </summary>
        [NotMapped]
        public string DisplayName => $"{FirstName} {LastName}";
    }

和 UserDto

public class UserDto
    {
        public UserDto()
        {
            Id = Guid.NewGuid().ToString();
            ConcurrencyStamp = Guid.NewGuid().ToString();
        }

        public string Id { get; set; }

        public string UserName { get; set; }

        public string NormalizedUserName => UserName.ToUpper();

        public string Email => UserName;

        public string NormalizedEmail => Email.ToUpper();

        public bool EmailConfirmed { get; set; }

        public string PasswordHash { get; set; }

        public string SecurityStamp { get; set; }

        public string ConcurrencyStamp { get; set; }

        public string PhoneNumber { get; set; }

        public bool PhoneNumberConfirmed { get; set; }

        public bool TwoFactorEnabled { get; set; }

        public DateTimeOffset? LockoutEnd { get; set; }

        public bool LockoutEnabled { get; set; }

        public int AccessFailedCount { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public byte[] ProfilePicture { get; set; }

        public DateTime? LastLogin { get; set; }

        public bool Enabled { get; set; }

        public List<string> Roles { get; set; }

        public List<Claim> Claims { get; set; }

        public List<string> Scopes { get; set; }
    }

加入UserRolesRoles ,所以使用組加入。

     var users = _ctx.Users
        .AsEnumerable()
        .GroupJoin(_ctx.UserClaims, u => u.Id, c => c.UserId, (user, claims) => new { user, claims })
        .GroupJoin(
            _ctx.UserRoles.Join(_ctx.Roles, ur => ur.RoleId, r => r.Id, (userRole, role) => new { userRole, role }),
            uc => uc.user.Id, r => r.userRole.UserId,
            (uc, roles) => new { uc.user, uc.claims, roles})
        .Select(ucr => new
        {
            Id = ucr.user.Id,
            UserName = ucr.user.UserName,
            FirstName = ucr.user.FirstName,
            LastName = ucr.user.LastName,
            LastLogin = ucr.user.LastLogin,
            LockoutEnd = ucr.user.LockoutEnd,
            LockoutEnabled = ucr.user.LockoutEnabled,
            Roles = ucr.roles.Select(x => x.role.Name).Distinct().ToList(),
            Scopes = ucr.claims.Select(x => x.ToClaim()).Where(x => x.Type.Equals(Shared.Scopes.Key)).Select(x => x.Value).ToList(),
            Claims = ucr.claims.Select(x => x.ToClaim()).Where(x => !x.Type.Equals(Shared.Scopes.Key)).ToList(),
            Enabled = ucr.user.Enabled
        }
    ).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM