首页 > 解决方案 > How to sort User and Role in core Identity

问题描述

I'm trying to display a list of Roles with users in my view.

This is my current output:

Role | Email

A | aaa@example.com

B | bbb@example.com

B | abc@example.com

C | baa@example.com

But I want it to be like this:

Role | Email

A | aaa@example.com

B | abc@example.com <--order by Role, then Email

B | bbb@example.com

C | baa@example.com

Any idea how to order the child in collection?

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();
        });
    }

标签: c#asp.net-core-identityasp.net-core-2.2

解决方案


Use ThenBy() method for the second sort column like below to have 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();

推荐阅读