如何获取ASP.NET MVC中的角色和用户数列表 [英] How do I get the list of role and number of users in them ASP.NET MVC
本文介绍了如何获取ASP.NET MVC中的角色和用户数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请问如何获取角色列表以及拥有该角色的用户数量?管理员3,员工7,主管12等等。
我尝试过:
Please how do I get the list of roles, along with the number of users having the role? something like Admin 3, Staff 7, Supervisor 12 etc.
What I have tried:
I have tried this
var users = allusers.Where(x=>x.Roles.Select(role => role.Name).Contains("User")).count;
but this is just for one role. I need it for all the roles, and when a new role is added in the db, it should also add to the list.
Thanks
推荐答案
您将了解如何实施。
为用户分配角色(C#)| Microsoft Docs [ ^ ]
You'll get an idea how to implement.
Assigning Roles to Users (C#) | Microsoft Docs[^]
您好,
尝试这样的方式
Hello,
Try something like this way
DataTable dtuserrole=new DataTable();
dtuserrole.Columns.Add("Name");
dtuserrole.Columns.Add("Role");
dtuserrole.Rows.Add("A","Admin");
dtuserrole.Rows.Add("B","Admin");
dtuserrole.Rows.Add("C","User");
dtuserrole.Rows.Add("D","User");
dtuserrole.Rows.Add("E","Stuff");
dtuserrole.Rows.Add("F","User");
dtuserrole.Rows.Add("G","Superuser");
dtuserrole.Rows.Add("H","User");
dtuserrole.AcceptChanges();
var results = (from p in dtuserrole.AsEnumerable()
group p by new {Role=p.Field<string>("Role")}into g
select new { Role = g.Key, Count = g.Count()});
和输出将是
and the output will be
foreach(var dr in results)
Console.WriteLine("Role: "+dr.Role + "; Count: "+dr.Count);
Role: { Role = Admin }; Count: 2
Role: { Role = User }; Count: 4
Role: { Role = Stuff }; Count: 1
Role: { Role = Superuser }; Count: 1
谢谢
Thanks
这篇关于如何获取ASP.NET MVC中的角色和用户数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文