What I'm trying to do is to is join tables to fill a viewModel that looks like this:
public class UserViewModel
{
public String Id { get; set; }
public String UserName { get; set; }
public String Email { get; set; }
public String Role { get; set; }
}
My query atm looks like this, but it doesn't work obviously, but it might help with descriping the problem.
public IActionResult AddAdmin()
{
var allUsers = (from u in _dbContext.Users
join r in _dbContext.UserRoles on u.Id equals r.UserId
join i in _dbContext.Roles on r.RoleId equals i.Id
select new UserViewModel
{
UserName = u.UserName,
Email = u.Email,
Id = u.Id,
Role = i.Name
}).ToList();
return View(allUsers);
}
As you see the thing I find hard is to apply the role to the viewModel, since they are connected to eachother with a manyTomany Table
Problem: The query does not work, and does not give anything in result Before I joined the role into the viewModel, i got the data to the view, now i get nothing at all.
My Question: What's the correct way to do this? To easily navigate through the data