Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Help - List out all Users with his Roles #367


User avatar
0
hole7 created

Dear all!

I'm using Abp Zero and I want to show all Users along with his Roles (RoleName) but with the current design, I can just see UserRole (RoleId) in User entity (I'm using Include to get UserRole list), but, my problem need to get all Roles that User has been assigned. So, Can anyone help me to figure out how to select a list of role to go with user? I've search on internet for join with EF but couldn't find the answer.

Thanks


1 Answer(s)
  • User Avatar
    0
    hole7 created

    Finally, I found the way to solve this.

    var queryUsers = _userRepository.GetAll().Include(u => u.Roles)
                                                .OrderBy(input.Sorting)
                                                .PageBy(input);
    
                var items = (from u in queryUsers
                             join ur in queryUsers.SelectMany(x => x.Roles) on u.Id equals ur.UserId
                             join r in _roleRepository.GetAll() on ur.RoleId equals r.Id
                             group new { u, ur, r } by new
                             {
                                 u.Id,
                                 u.UserName,
                                 u.Name,
                                 u.Surname,
                                 u.EmailAddress,
                                 u.IsEmailConfirmed,
                                 u.IsActive,
                                 u.LastLoginTime,
                                 u.CreationTime
                             } into g
                             select new UserDto()
                             {
                                 Id = g.Key.Id,
                                 UserName = g.Key.UserName,
                                 Name = g.Key.Name,
                                 Surname = g.Key.Surname,
                                 EmailAddress = g.Key.EmailAddress,
                                 IsEmailConfirmed = g.Key.IsEmailConfirmed,
                                 IsActive = g.Key.IsActive,
                                 LastLoginTime = g.Key.LastLoginTime,
                                 CreationTime = g.Key.CreationTime,
                                 Roles = g.Select(x => new UserRoleDto()
                                 {
                                     RoleId = x.r.Id,
                                     RoleName = x.r.DisplayName
                                 }).ToList()
                             }).ToList();