mvc 5 roles slect list user code example

Example: User List get Role Name mvc 5

var usersWithRoles = (from user in context.Users  
                                  select new  
                                  {  
                                      UserId = user.Id,                                        
                                      Username = user.UserName,  
                                      Email = user.Email,  
                                      RoleNames = (from userRole in user.Roles  
                                                   join role in context.Roles on userRole.RoleId   
                                                   equals role.Id  
                                                   select role.Name).ToList()  
                                  }).ToList().Select(p => new Users_in_Role_ViewModel()  
   
                                  {  
                                      UserId = p.UserId,  
                                      Username = p.Username,  
                                      Email = p.Email,  
                                      Role = string.Join(",", p.RoleNames)  
                                  });  
   
   
            return View(usersWithRoles);

Tags:

Misc Example