I'm sorry for my poor command of English.
To display a list of users for each consultant, I need to first display the first name, last name, phone number and field on a page in ASP.NET Core 5.
To do this, I need to be able to merge AspNetUsers
and Students
tables so that I can take information from them and save it in the view model so that I can show it on one page.
But the problem is that I can not merge these two tables. I even used the Join method in Linq, but I got an error.
Needless to say, these two tables are already relationshiped and the only problem is the simultaneous display of information from these two contexts - one IdentityDbContext
and other is PlanContext
.
Thank you!
View model Adminshowusers
:
public class Adminshowusers
{
public string FirstName { get; set; }
public string LastName{ get; set; }
public string PhoneNumber{ get; set; }
public string Field { get; set; }
}
And my action method that did not work
public async Task<IActionResult> ConsultantIndex(string Msg,string id)
{
var data = await _userManager.GetAllUsersWithRolesAsync();
var users = (from u in data
join a in _context.Students
on data.Select(x=>x.Id) equals a.ID
select new AdminAddUsers()
{
FirstName = u.FirstName,
LastName = u.LastName,
PhoneNumber = u.PhoneNumber,
Field = a.Field
});
return View(users);
}
Method GetAllUsersWithRolesAsync()
is also pre-created and displays a list of all users along with their roles:
public async Task<List<UsersViewModel>> GetAllUsersWithRolesAsync()
{
return await Users.Select(user => new UsersViewModel
{
Id = user.Id,
Email = user.Email,
UserName = user.UserName,
PhoneNumber = user.PhoneNumber,
FirstName = user.FirstName,
LastName = user.LastName,
BirthDate = user.BirthDate,
IsActive = user.IsActive,
LastVisitDateTime = user.LastVisitDateTime,
Image = user.Image,
RegisterDate = user.RegisterDate,
Roles = user.Roles.Select(u => u.Role.Name),
}).ToListAsync();
}
CodePudding user response:
I would change code a little, you can't join code in server with code in database. At first bring all students from db and after this join with another list.
var data = await _userManager.GetAllUsersWithRolesAsync();
var students = await _context.Students.Select(i=> new {ID=i.ID, Field=i.Field}).ToListAsync();
var users = (from u in data
join a in students on u.Id equals a.ID
select new AdminAddUsers()
{
FirstName=u.FirstName,
LastName=u.LastName,
PhoneNumber = u.PhoneNumber,
Field =a.Field
}).ToList();
return View(users);