如何对核心身份中的用户和角色进行排序

How to sort User and Role in core Identity

我正在尝试显示我认为有用户的角色列表。

这是我当前的输出:

角色 |电子邮件

一个 | aaa@example.com

乙 | bbb@example.com

乙 | abc@example.com

C |咩@example.com

但我希望它是这样的:

角色 |电子邮件

一个 | aaa@example.com

乙 | abc@example.com <--按角色排序,然后是电子邮件

乙 | bbb@example.com

C |咩@example.com

知道如何在 collection 中订购 child 吗?

SettingController.cs

    private readonly UserManager<ApplicationUser> _userManager;
    private readonly RoleManager<ApplicationRole> _roleManager;

    public SettingController(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
    {
        _userManager = userManager;
        _roleManager = roleManager;
    }

    public IActionResult Index()
    {
        var usersWithRole = _roleManager.Roles.Include(r => r.UserRoles).ThenInclude(u => u.User).OrderBy(r => r.Name).ToList();

        return View(usersWithRole);
    }

Index.cshtml

@{
    ViewData["Title"] = "Index";
    ViewData["Name"] = "Index";
    int count = 1;
}
<table class="table table-striped bg-white">
    <thead>
        <tr>
            <th>No</th>
            <th>Role</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var role in Model)
        {
            var result = role.UserRoles;
            @foreach(var userRole in result)
            {
                <tr>
                    <td>@count</td>
                    <td>@role.Name</td>
                    <td>@userRole.User</td>
                </tr>
                count++;
            }   
        }
    </tbody>
</table>

ApplicationRole.cs

public class ApplicationUser : IdentityUser
{
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

ApplicationUserRole.cs

public class ApplicationUserRole : IdentityUserRole<string>
{
    public virtual ApplicationUser User { get; set; }
    public virtual ApplicationRole Role { get; set; }
}

ApplicationUser.cs

public class ApplicationUser : IdentityUser
{
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

ApplicationDbContext.cs

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<ApplicationUserRole>(userRole =>
        {
            userRole.HasKey(ur => new { ur.UserId, ur.RoleId });

            userRole.HasOne(ur => ur.Role)
                .WithMany(r => r.UserRoles)
                .HasForeignKey(ur => ur.RoleId)
                .IsRequired();

            userRole.HasOne(ur => ur.User)
                .WithMany(r => r.UserRoles)
                .HasForeignKey(ur => ur.UserId)
                .IsRequired();
        });
    }

对第二个排序列使用 ThenBy() 方法,如下所示 order by Name, Email asc

 var usersWithRole = _roleManager.Roles.Include(r => r.UserRoles)
                      .ThenInclude(u => u.User)
                      .OrderBy(r => r.Name)
                      .ThenBy(l => l.Email)
                      .ToList();

我设法找到了一种方法来获得我想要的结果。

我为表创建实体并通过 linq 访问它,而不是使用 Identity 的 UserManager 和 RoleManager。

然后,我将结果绑定到 ViewModel 并将其显示在我的视图中。

控制器

public IActionResult Index()
    {

        //var usersWithRole = _roleManager.Roles.Include(r => r.UserRoles).ThenInclude(u => u.User).OrderBy(r => r.Id).ToList();

        var roleAndUsers = (from r in _context.Role
                     join ur in _context.UserRole
                     on r.Id equals ur.RoleId
                     join u in _context.User
                     on ur.UserId equals u.Id
                     orderby r.Name, u.Email
                     select new UserAndRoleViewModel { Name = r.Name, Email = u.Email, Id = u.Id }).ToList();

        return View(roleAndUsers);
    }

视图模型

public class UserAndRoleViewModel
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Id { get; set; }
}

查看

@{
    ViewData["Title"] = "Index";
    int count=1;
}
<table class="table table-striped bg-white">
    <thead>
        <tr>
            <th>No</th>
            <th>Role</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var role in Model)
        {
            <tr>
                <td>@count</td>
                <td>@role.Name</td>
                <td>@role.Email</td>
            </tr>
            count++;
        }
    </tbody>
</table>

ApplicationDbContext

    public DbSet<ApplicationRole> Role { get; set; }
    public DbSet<ApplicationUserRole> UserRole { get; set; }
    public DbSet<ApplicationUser> User { get; set; }