如果未找到项目,LINQ 表达式可以使用 LEFT JOIN 或默认为 null 吗?
我正在尝试追踪我继承的 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();
错误是“序列不包含元素”,我已将其追踪到此line:
join pto in context.TPM_USER on v.TPM_USER1.USERID equals pto.USERID
如果 USER1 为空,则不会返回该行。相反,我需要 PrimaryTrainingOwner 为 null 或空白。基本上,我需要执行 LEFT JOIN,并在设置 PrimaryTrainerOwner 时检查这一点。有没有办法更新这个 LINQ 表达式来处理这种情况?谢谢!
I'm trying to track down a crash in some existing code in a web app I inherited.. I've traced this down to a single LINQ expression:
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();
The error is "Sequence contains no elements", which I've traced to this line:
join pto in context.TPM_USER on v.TPM_USER1.USERID equals pto.USERID
If USER1 is null, the row does not get returned. Instead, I need PrimaryTrainingOwner to just be null or blank instead. Basically, I need to do a LEFT JOIN instead and check for this while setting PrimaryTrainerOwner. Is there a way to update this LINQ expression to handle this case? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我找到了解决方案。对于这是否是最好的方法或语法或其他什么的任何评论,我将不胜感激。谢谢!
I think I found a solution. I'd appreciate any comments on whether this is the best approach or syntax or whatever. Thanks!