I'm able to use LINQ to Entities to return rows that have, for example, userId 1 with RoleNames = Manager;User;Recorder.
var tempusersByBranch = (from u in _db.VRT_User
join urb in _db.VRT_UserRoleBranch on u.UserId equals urb.UserId
join r in _db.VRT_Role on urb.RoleId equals r.RoleId
where branches.Contains(urb.BranchId)
group r by new { urb.UserId, urb.BranchId } into rg
select new
{
UserId = rg.Key.UserId,
BranchId = rg.Key.BranchId,
RoleNames = rg.Select(r => r.RoleName)
});
var tempusersBybranch2 = (from t in tempusersByBranch.ToList()
select new UserRoleBranchModel
{
UserId = t.UserId,
BranchId = t.BranchId,
RoleNames = String.Join(";", t.RoleNames.ToArray())
});
This is a temp workaround to deal with "LINQ to Entities does not recognize the method 'System.String Join'." usersByBranch does return the correct results.
var usersByBranch = (from t in tempusersBybranch2
select new UserRoleBranchModel
{
UserId = t.UserId,
BranchId = t.BranchId,
RoleNames = t.RoleNames
}).AsQueryable();
When the code below is executed, I get this error message: Unable to create a constant value of type 'xxx.Models.UserRoleBranchModel'. Only primitive types or enumeration types are supported in this context. The issue has to do with join urb in usersByBranch on u.UserId equals urb.UserId. Despite googling, I can't figure out the problem.
var usersByRole = (from u in _db.VRT_User
join urb in usersByBranch on u.UserId equals urb.UserId
where urb.RoleName == "User"
|| urb.RoleName =="Administrator"
select new { u.LastName, u.FirstName, u.UserId, u.UserEmail, u.IsDisabled, u.CreatedDate, u.UpdatedDate, u.CreatedById, u.UpdatedById, urb.RoleNames });
Your help is appreciated.