如何将这些 LINQ 连接转换为 LEFT OUTER 连接?

发布于 2024-07-27 00:44:15 字数 1412 浏览 9 评论 0原文

var auditAgencyRecords = (from ag in db.Agencies
                                      join ara in db.AuditRuleAccounts on ag.Agency_Id equals ara.AgencyID
                                      join arr in db.AuditRuleResults on ara.AuditRuleAccountID equals arr.AuditRuleAccountID
                                      join are in db.AuditRuleEnterprises on arr.AuditRuleEnterpriseID equals are.AuditRuleEnterpriseID
                                      where (rules.Select(r => r.EnterpriseID).Contains(arr.AuditRuleEnterpriseID))
                                      select new
                                      {

                                          AgencyID = ag.Agency_Id,
                                          AgencyName = ag.Agency_Name,
                                          AuditRuleEnterpriseID = arr.AuditRuleEnterpriseID,
                                          AuditRuleEnterpriseName = are.OverrideDisplayName,
                                          CorrectedDate = arr.CorrectedDate,
                                          NbrDaysToCorrect = arr.NbrDaysToCorrect,

                                      });

因此,我对 LINQ to SQL 还很陌生,我需要帮助弄清楚如何将其转换为左外连接,而不是内连接。

从上面的查询中,我希望获得机构表中的所有机构,然后右侧的记录(EnterpriseID、CorretedDate 等)可以为空,如果不存在记录,则以此类推。

我相当确定我需要使用 SelectMany,但我可以使用一些指导将这些联接转换为左外联接,因此我有一个所有机构的列表,然后在右侧列出了它们的可能记录。

var auditAgencyRecords = (from ag in db.Agencies
                                      join ara in db.AuditRuleAccounts on ag.Agency_Id equals ara.AgencyID
                                      join arr in db.AuditRuleResults on ara.AuditRuleAccountID equals arr.AuditRuleAccountID
                                      join are in db.AuditRuleEnterprises on arr.AuditRuleEnterpriseID equals are.AuditRuleEnterpriseID
                                      where (rules.Select(r => r.EnterpriseID).Contains(arr.AuditRuleEnterpriseID))
                                      select new
                                      {

                                          AgencyID = ag.Agency_Id,
                                          AgencyName = ag.Agency_Name,
                                          AuditRuleEnterpriseID = arr.AuditRuleEnterpriseID,
                                          AuditRuleEnterpriseName = are.OverrideDisplayName,
                                          CorrectedDate = arr.CorrectedDate,
                                          NbrDaysToCorrect = arr.NbrDaysToCorrect,

                                      });

So, I'm still pretty new to LINQ to SQL and I need help figuring out how to turn this into a left outer join, rather than an inner join.

From that query above, I'm looking to have ALL agencies from the Agencies table, and then the records on the right (EnterpriseID, CorrectedDate, etc) can be null, etc if no records exist.

I'm fairly certain I need to use SelectMany but I could use some guidance turning those joins into LEFT OUTER JOINS, so I have a list of all agencies then their possible records on the right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

赠佳期 2024-08-03 00:44:15

下面是一些可以帮助您入门的代码。

var auditAgencyRecords = (
from ag in db.Agencies
group join ara in db.AuditRuleAccounts on ag.Agency_Id equals ara.AgencyID into AgAra = group
from w in AgAra.DefaultIfEmpty()
group join arr in db.AuditRuleResults on ara.AuditRuleAccountID equals arr.AuditRuleAccountID into AraArr = group
from x in AraArr.DefaultIfEmpty()
group join are in db.AuditRuleEnterprises on arr.AuditRuleEnterpriseID equals are.AuditRuleEnterpriseID into ArrAre = group
from y in ArrAre.DefaultIfEmpty()
where (rules.Select(r => r.EnterpriseID).Contains(arr.AuditRuleEnterpriseID))
select new
{    
     AgencyID = w.Agency_Id,
     AgencyName = w.Agency_Name,                                           
     AuditRuleEnterpriseID = y.AuditRuleEnterpriseID,
     AuditRuleEnterpriseName = y.OverrideDisplayName,
     CorrectedDate = w.CorrectedDate,
     NbrDaysToCorrect = w.NbrDaysToCorrect,
});

Below is some code that can get you started.

var auditAgencyRecords = (
from ag in db.Agencies
group join ara in db.AuditRuleAccounts on ag.Agency_Id equals ara.AgencyID into AgAra = group
from w in AgAra.DefaultIfEmpty()
group join arr in db.AuditRuleResults on ara.AuditRuleAccountID equals arr.AuditRuleAccountID into AraArr = group
from x in AraArr.DefaultIfEmpty()
group join are in db.AuditRuleEnterprises on arr.AuditRuleEnterpriseID equals are.AuditRuleEnterpriseID into ArrAre = group
from y in ArrAre.DefaultIfEmpty()
where (rules.Select(r => r.EnterpriseID).Contains(arr.AuditRuleEnterpriseID))
select new
{    
     AgencyID = w.Agency_Id,
     AgencyName = w.Agency_Name,                                           
     AuditRuleEnterpriseID = y.AuditRuleEnterpriseID,
     AuditRuleEnterpriseName = y.OverrideDisplayName,
     CorrectedDate = w.CorrectedDate,
     NbrDaysToCorrect = w.NbrDaysToCorrect,
});
给妤﹃绝世温柔 2024-08-03 00:44:15

这里是如何执行此操作的一个很好的示例。
重要的是不要忘记这些可能带回来的空值——这曾经让我很难过。

Here is a good example of how to do it.
It's important to not forget about null values that these can bring back -- this bit me hard once.

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