如何将这些 LINQ 连接转换为 LEFT OUTER 连接?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是一些可以帮助您入门的代码。
Below is some code that can get you started.
这里是如何执行此操作的一个很好的示例。
重要的是不要忘记这些可能带回来的空值——这曾经让我很难过。
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.