Home > Mobile >  Get nested UserRoles from User table
Get nested UserRoles from User table

Time:12-01

I am querying from Database and there are nested tables. My query looks like this:

  System.Collections.Generic.IEnumerable<ApplicationUser> applicationUsers = await _userManager.Users
        .Include(u => u.UserRoles).ToListAsync();

I would like to get all UserRole ID's and role names, so I have done this:

  IEnumerable<List<UserRole>> userRoles = applicationUsers.Select(person => person.UserRoles.Select(u => new UserRole
        {
          Id = u.RoleId,
          RoleName = u.Role.ToString(),
        }).ToList());

However as an output I am getting IEnumerable<List<UserRole>> when I need List<UserRole>. What I am doing wrong?

UserRole.cs:

  public class UserRole
  {
    public string Id { get; set; }
    public string RoleName { get; set; }
    public bool Successful { get; set; }

  }

CodePudding user response:

Use

List<UserRole> userRoles = applicationUsers.SelectMany(person => person.UserRoles).ToList();
  • Related