我有5个表Workflow为'A',WorkflowSteps为'B',AppUser为'C',AppRoles为'D'和AppDepartment为'E'我想以这种方式获取记录
1)根据工作流ID匹配A和B中的记录
2)匹配来自B和C,B和D,B和E的记录,因为表B具有外键UserId,RoleId和DepartmentId
但是问题是B中的某些列UserId为null,有些DepartmentId为Null,有些RoleId为Null,所以内部联接将不起作用
这是我的查询,它将必须获取三行,但是我得到0
var workflows = (from q in _context.WorkFlowSteps
join p in _context.WorkFlow
on q.WorkFlowId equals p.WorkFlowId
join r in _context.AppUsers
on q.ApprovalUserId equals r.UserId
from dep in _context.AppDepartment
from role in _context.AppRoles
where (q.DepartmentId == dep.DepartmentId) &&
(q.RoleId == role.RoleId)&&
(q.WorkFlowId == id &&
q.IsAllowed == true &&
p.WebSiteId == WebsiteId)
select new WorkFlowViewModel
{
UserId =Convert.ToInt32(q.ApprovalUserId.HasValue?
q.ApprovalUserId:
(q.DepartmentId.HasValue?
q.DepartmentId:
q.RoleId)),
ModeId = p.ModeId,
WebSiteId = p.WebSiteId,
Step = q.StepNo,
Name =q.ApprovalUserId.HasValue ?
p.Name :
(q.DepartmentId.HasValue?
dep.Name :
r.Name),
WorkFlowId = p.WorkFlowId
}).ToList();
答案 0 :(得分:1)
假设WorkFlowViewModel,WorkFlow,WorkFlowSteps,AppUsers,AppDepartment,AppRoles的类(最少)(根据您的Linq查询推断):
public class WorkFlow
{
public int WorkflowId { get; set; }
public virtual IQueryable<WorkFlowSteps> WorkFlowSteps { get; set; }
public int WebSiteId { get; set; }
public int ModeId { get; set; }
}
public class WorkFlowSteps
{
public int Id { get; set; }
public int StepNo { get; set; }
public bool IsAllowed { get; set; }
public int WorkFlowId { get; set; }
public WorkFlow WorkFlow { get; set; }
public int? AppUserId { get; set; }
public virtual AppUser AppUser { get; set; }
public int? DepartmentId { get; set; }
public virtual AppDepartment AppDepartment { get; set; }
public int? RoleId { get; set; }
public virtual AppRole AppRole { get; set; }
}
public class AppUser
{
public int Id { get; set; }
public List<WorkFlowSteps> WorkFlowSteps { get; set; }
public string Name { get; set; }
}
public class AppDepartment
{
public int Id { get; set; }
public List<WorkFlowSteps> WorkFlowSteps { get; set; }
public string Name { get; set; }
}
public class AppRole
{
public int Id { get; set; }
public List<WorkFlowSteps> WorkFlowSteps { get; set; }
public string Name { get; set; }
}
public class WorkFlowViewModel
{
public int WorkFlowId { get; set; }
public int Step { get; set; }
public int ModeId { get; set; }
public int WebSiteId { get; set; }
public int ApproverId { get; set; }
public string ApproverName { get; set; }
}
然后,此代码应选择然后映射您的数据。第一步使用EF和导航路径选择数据。第二步创建所需的输出或“装饰对象”。
// Inputs consist of workflow id & website id & EF context
var selectedRecords = _context.WorkFlows
.Include("WorkFlowSteps")
.Include("WorkFlowSteps.AppUser")
.Include("WorkFlowSteps.AppDepartment")
.Include("WorkFlowSteps.AppRole")
.Select(r => r.Id == id && r.WebSiteId == websiteId && r.WorkFlowSteps.IsAllowed);
var myModel = (from q in selectedRecords
select new WorkFlowViewModel
{
ApproverId = Convert.ToInt32(q.WorkFlowStep.AppUserId.HasValue
? q.WorkFlowStep.AppUserId
: (q.WorkFlowStep.AppDepartmentId.HasValue
? q.WorkFlowStep.AppDepartmentId
: (q.WorkFlowStep.AppRoleId.HasValue ? q.WorkFlowStep.AppRoleId, "0"))),
ModeId = q.ModeId,
WebSiteId = q.WebSiteId,
Step = q.WorkFlowStep.StepNo,
ApproverName = q.WorkFlowStep.AppUserId.HasValue
? q.WorkFlowStep.AppUser.Name
: (q.WorkFlowStep.AppDepartmentId.HasValue
? q.WorkFlowStep.AppDepartment.Name
: (q.WorkFlowStep.AppRoleId.HasValue ? q.WorkFlowStep.AppRole.Name, "")),
WorkFlowId = q.WorkFlowId
}).ToList();