如果未找到项目,LINQ 表达式可以使用 LEFT JOIN 或默认为 null 吗?

发布于 2024-12-15 00:50:31 字数 1206 浏览 0 评论 0原文

我正在尝试追踪我继承的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2024-12-22 00:50:31

我想我找到了解决方案。对于这是否是最好的方法或语法或其他什么的任何评论,我将不胜感激。谢谢!

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();

I think I found a solution. I'd appreciate any comments on whether this is the best approach or syntax or whatever. Thanks!

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文