我需要帮助来使用带有WHERE子句的LINQ来执行JOIN。 问题是当事件中的CaseId为空时(并非所有事件都与案例相关) 它导致NOT显示事件。
这是我的代码:
var queryEvents = (from e in db.events
join u in db.users on e.UserID equals u.UserID
join c in db.cases on e.CaseID equals c.CaseID
where e.UserID == Program.loggedUser.UserID || (e.UserGroupID == Program.loggedUser.UserGroupID && c.AccessLvl>0)
select new { User = u.FirstName + " " + u.LastName, e.Name, e.Description, e.StartDate }).OrderByDescending(x => x.StartDate);
gvAppointments.DataSource = queryEvents.ToList();
我找到了一些关于如何使用INTO使用LINQ LEFT JOIN的例子,但后来我对WHERE语句有疑问。老实说,我不知道把它放在哪里。
有人可以帮帮我吗?
答案 0 :(得分:0)
var queryEvents = (from e in db.events
join u in db.users on e.UserID equals u.UserID
join c in db.cases on e.CaseID equals c.CaseID into cases
from subC in cases.DefaultIfEmpty()
where e.UserID == Program.loggedUser.UserID || (e.UserGroupID == Program.loggedUser.UserGroupID && subC?.AccessLvl ?? 0 > 0)
select new { User = u.FirstName + " " + u.LastName, e.Name, e.Description, e.StartDate }).OrderByDescending(x => x.StartDate);
gvAppointments.DataSource = queryEvents.ToList();
您只需要知道,subC现在可能为null,因此您必须使用默认值,访问它的属性。