How to list users with role names in ASP.NET MVC 5

后端 未结 5 1133
栀梦
栀梦 2021-02-02 18:10

I have default project template of ASP.NET MVC 5 web site and I am trying to list all users with role names (not IDs).

The query is:

db.Users.Include(u =         


        
5条回答
  •  花落未央
    2021-02-02 18:25

    using System.Linq;
    using System.Data;
    using System.Data.Entity;
    
                var db = new ApplicationDbContext();
                var Users = db.Users.Include(u => u.Roles);
    
                foreach (var item in Users)
                {
                    string UserName = item.UserName;
                    string Roles = string.Join(",", item.Roles.Select(r=>r.RoleId).ToList());
                }
    

提交回复
热议问题