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,CorrectedDate等)可以为null等。
我很确定我需要使用SelectMany但是我可以使用一些指导将这些连接转换为LEFT OUTER JOINS,所以我有一个所有代理商的列表,然后是他们在右边的可能记录。
答案 0 :(得分:2)
以下是一些可以帮助您入门的代码。
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,
});
答案 1 :(得分:0)
Here是如何做到的一个很好的例子。 重要的是不要忘记这些可以带回来的空值 - 这一点我很难。