我正在尝试追踪我继承的Web应用程序中某些现有代码的崩溃。我已经将其追溯到单个LINQ表达式:
var projInfo = (from v in context.TPM_PROJECTVERSION
join p in context.TPM_PROJECT on v.PROJECTID equals p.PROJECTID
join pto in context.TPM_USER on v.TPM_USER1.USERID equals pto.USERID
join pt in context.TPM_PROJECTTYPES on p.PROJECTTYPEID equals pt.PROJECTTYPEID
where v.PROJECTID == projectId && v.VERSIONID == versionId
select new
{
ProjectName = v.NAME,
ProjectType = pt.SHORTNAME,
ProjectDesc = v.DESCRIPTION,
BusinessLaunchData = p.BUSINESSLAUNCHDATE,
BusinessSponsor = (v.TPM_USER3.FIRSTNAME + " " + v.TPM_USER3.LASTNAME),
PrimaryTrainingOwner = (pto.FIRSTNAME + " " + pto.LASTNAME)
}).First();
错误是“序列不包含任何元素”,我已追溯到这一行:
join pto in context.TPM_USER on v.TPM_USER1.USERID equals pto.USERID
如果USER1为null,则不返回该行。相反,我需要将PrimaryTrainingOwner改为null或空白。基本上,我需要进行LEFT JOIN,并在设置PrimaryTrainerOwner时检查这一点。有没有办法更新此LINQ表达式来处理这种情况?谢谢!
答案 0 :(得分:1)
我想我找到了解决方案。关于这是最好的方法或语法还是其他什么,我感激不尽。谢谢!
var projInfo = (from v in context.TPM_PROJECTVERSION
join p in context.TPM_PROJECT on v.PROJECTID equals p.PROJECTID
join pto in context.TPM_USER on v.TPM_USER1.USERID equals pto.USERID into primaryowner
from subpto in primaryowner.DefaultIfEmpty()
join pt in context.TPM_PROJECTTYPES on p.PROJECTTYPEID equals pt.PROJECTTYPEID
where v.PROJECTID == projectId && v.VERSIONID == versionId
select new
{
ProjectName = v.NAME,
ProjectType = pt.SHORTNAME,
ProjectDesc = v.DESCRIPTION,
BusinessLaunchData = p.BUSINESSLAUNCHDATE,
BusinessSponsor = (v.TPM_USER3.FIRSTNAME + " " + v.TPM_USER3.LASTNAME),
PrimaryTrainingOwner = subpto == null ? String.Empty : (subpto.FIRSTNAME + " " + subpto.LASTNAME)
}).First();