79716779

Date: 2025-07-28 00:21:42
Score: 0.5
Natty:
Report link

thank you zhi-lv !

i got this working as method in DBContext:

public class ApplicationUser : IdentityUser
{
    [NotMapped]
    public List<IdentityRole>? Roles { get; set; }
}
public async Task<List<ApplicationUser>> GetUsersByRole(params string[] roles)
{
    List<ApplicationUser>? res = await Users
    .Join(UserRoles, u => u.Id, ur => ur.UserId, (u, ur) => new { u, ur })
    .Join(Roles.Where(r => roles.Contains(r.Name)), ur => ur.ur.RoleId, r => r.Id, (ur, r) => new { ur, r })
    .GroupBy(uv => new { uv.ur.u.UserName, uv.ur.u.Email }).Select(r => new ApplicationUser()
    {
        UserName = r.Key.UserName,
        Email = r.Key.Email,
        Roles = r.Select(c=>c.r).ToList()
    }).ToListAsync();

    return res;
}
Reasons:
  • Blacklisted phrase (0.5): thank you
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: 3263927 contra