简体   繁体   中英

Get list of Users with Roles without DBContext in asp.net mvc

I want to show users in a table with their respective roles (if there are multiple roles assigned). all i got users but role comes empty.

Here is my Model

public class AllUsers
{
    public string ID { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Role { get; set; }
} 

Here is my code:

var usersWithRoles = (from user in context.Users
                              select new
                              {
                                  UserId = user.Id,
                                  Username = user.UserName,
                                  Email = user.Email,
                                  RoleNames = (from userRole in user.Roles
                                               join role in context.Roles on userRole.RoleId
                                               equals role.Id
                                               select role.Name).ToList()
                              }).ToList().Select(p => new AllUsers()

                              {
                                  ID = p.UserId,
                                  UserName = p.Username,
                                  Email = p.Email,
                                  Role = string.Join(",", p.RoleNames)
                              });

quick help needed,

I tried with join but still no result here is the code

(from user in context.Users
                  from userRole in user.Roles
                  join role in context.Roles on userRole.RoleId equals 
                  role.Id
                  select new AllUsers()
                  {
                      ID = user.Id,
                      UserName = user.UserName,
                      Email = user.Email,
                      Role = role.Name
                  }).ToList();

Use the Include method, which will join users and Roles together before running your query:

var usersWithRoles = (from user in context.Users.Include(u => u.Roles)
                                 select new
                                 {
                                     UserId   = user.Id,
                                     Username = user.UserName,
                                     Email    = user.Email,
                                     RoleNames = (from userRole in user.Roles
                                                  join role in context.Roles on userRole.RoleId
                                                      equals role.Id
                                                  select role.Name).ToList()
                                 }).ToList().Select(p => new AllUsers()
                                 {
                                    ID = p.UserId,
                                    UserName = p.Username,
                                    Email = p.Email,
                                    Role = string.Join(",", p.RoleNames)
                                 })

Also make sure that in your DBContext Users model, you have the collection of Roles , like this:

public class Users {
  // ... other members

  public ICollection<Roles> Roles { get; set; }
}

Btw, it's worth to mention that with the same logic Roles (taking into account the relations Many to One) should also contain a User :

public class Roles {
  // ... other members

  public User User { get; set; }
}

One side note: it's better to call ToList() at the end of your query but not in the middle:

var usersWithRoles = (...Select).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM