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

时间:2019-03-21 08:29:52

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

我正在尝试在用户视图中显示角色列表。

这是我当前的输出:

角色|电子邮件

A | aaa@example.com

B | bbb@example.com

B | abc@example.com

C | baa@example.com

但是我希望它像这样:

角色|电子邮件

A | aaa@example.com

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

B | bbb@example.com

C | baa@example.com

有什么想法如何订购孩子吗?

SettingController.cs

df <- structure(list(one = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label 
 = c("a", 
"b"), class = "factor"), two = c(1L, 2L, 3L, 1L, 2L, 3L), three = 
structure(c(2L, 
2L, 1L, 2L, 1L, 1L), .Label = c("no", "yes"), class = "factor")), 
row.names = c("1:", 
"2:", "3:", "4:", "5:", "6:"), class = "data.frame")

Index.cshtml

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

ApplicationRole.cs

@{
    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>

ApplicationUserRole.cs

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

ApplicationUser.cs

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

ApplicationDbContext.cs

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

2 个答案:

答案 0 :(得分:2)

对第二个排序列使用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();

答案 1 :(得分:0)

我设法找到一种获得所需结果的方法。

我为表创建实体,并通过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);
    }

ViewModel

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