Linq left outer join 和 union 返回许多记录
Linq left outer join and a union returning to many records
我正在将一个运行良好的存储过程转换为 linq 查询,但没有得到正确的记录。
存储过程如下所示:
SELECT isnull(a.CertificationId, cc.CertificationId) as CertificationId,
isnull(a.cipcode, cc.CipCode) as CipCode,isnull(a.Credential, lc.Credential) as Credential,
lc.LicensingCertificationProgram, lc.IssuingOrganization, isnull(a.ab, '') as ab
FROM enrCertificationCipCodes CC INNER JOIN
lkpCertifications lc ON CC.CertificationId = lc.CertificationId
left join
(SELECT pc.CertificationId,
cc.CipCode,
lc.Credential,
lc.LicensingCertificationProgram,
lc.IssuingOrganization, ab = 'Yes',
pc.psn
FROM enrCertificationCipCodes CC INNER JOIN
lkpCertifications lc ON CC.CertificationId = lc.CertificationId
inner join (select * from enrProgramCertifications where PSN = @PSN) PC on cc.CertificationId = pc.CertificationId ) as a on a.CertificationId = cc.CertificationId
where cc.CipCode = @CipCode
union
select pc.CertificationId as CertificationId, p.cipcode as CipCode, other as Credential,'' as LicensingCertificationProgram,
'' as IssuingOrganization, 'Yes' as ab
from (select * from enrProgramCertifications where CertificationId = '99999') pc join enrProgram p on pc.PSN = p.PSN
where p.CIPCode = @CipCode and p.PSN = @PSN
Union
select '99999' as CertificationId, @CipCode as CipCode, 'Other' as Credential,'' as LicensingCertificationProgram, '' as IssuingOrganization, '' as ab
order by ab desc, Credential
我的 linq 查询是
var t = ((from a in LkpCertifications
join d in EnrProgramCertifications on a.CertificationId equals d.CertificationId into ad
from d in ad.DefaultIfEmpty()
join c in EnrCertificationCipCodes on a.CertificationId equals c.CertificationId
where c.CipCode == "52.1999"
select new {
CertificationId = a.CertificationId,
CipCode = c.CipCode,
Credential = a.Credential,
Licensing = a.LicensingCertificationProgram,
IssuingOrganization = a.IssuingOrganization,
Psn = d.PSN != null ? d.PSN : 0,
ab = d.PSN != null ? "Yes" : ""
})
.Union (from k in EnrProgramCertifications
join l in LkpCertifications on k.CertificationId equals l.CertificationId where k.PSN == 19480
select new{
CertificationId = k.CertificationId,
CipCode = "52.1999",
Credential = k.Other,
Licensing = l.LicensingCertificationProgram,
IssuingOrganization = l.IssuingOrganization,
Psn = k.PSN != null ? k.PSN : 0,
ab = k.PSN != null ? "Yes" : ""
})).ToList();
t.Dump();
结果是我从左侧加入了更多行 table。如果我尝试通过使用 PSN 号码限定它来限制从左侧加入 table 的行,那么我不会从 lkpCertifications table 获得结果。 lkpCertifications 包含所有证书,而 enrProgramCertifications 仅包含那些已被选中的证书,就像订单和订单详细信息设置一样。
所以我目前的结果看起来像
我应该只得到一个 PSN 为 0 的项目 54 和一个 PSN 为 19480 的 55 记录。
对这个问题有什么想法吗?
我能够通过使用一对连接到对象中来解决这个问题,这些对象 defaultifempty() 到 return 左外连接。
certs = (from a in _context.LkpCertifications
join d in _context.EnrProgramCertifications on a.CertificationId equals d.CertificationId into ad
from d in ad.DefaultIfEmpty()
join c in _context.EnrCertificationCipCodes on a.CertificationId equals c.CertificationId into ac
from c in ac.DefaultIfEmpty()
where c.CipCode == CIP || d.Psn == Psn
select new vmManageCertifications
{
Psn = d.Psn == Psn ? d.Psn : 0,
Cipcode = c.CipCode == null ? "0" : c.CipCode,
CertificationId = a.CertificationId,
Credential = a.Credential == "Other" ? d.Other : a.Credential,
LicensingCertificationProgram = a.LicensingCertificationProgram,
IssuingOrganization = a.IssuingOrganization,
ProgramCertified = d.Psn == Psn ? true : false
}).ToList().GroupBy(i => i.CertificationId).Select(group => group.First()).OrderByDescending(i => i.ProgramCertified);
我正在将一个运行良好的存储过程转换为 linq 查询,但没有得到正确的记录。 存储过程如下所示:
SELECT isnull(a.CertificationId, cc.CertificationId) as CertificationId,
isnull(a.cipcode, cc.CipCode) as CipCode,isnull(a.Credential, lc.Credential) as Credential,
lc.LicensingCertificationProgram, lc.IssuingOrganization, isnull(a.ab, '') as ab
FROM enrCertificationCipCodes CC INNER JOIN
lkpCertifications lc ON CC.CertificationId = lc.CertificationId
left join
(SELECT pc.CertificationId,
cc.CipCode,
lc.Credential,
lc.LicensingCertificationProgram,
lc.IssuingOrganization, ab = 'Yes',
pc.psn
FROM enrCertificationCipCodes CC INNER JOIN
lkpCertifications lc ON CC.CertificationId = lc.CertificationId
inner join (select * from enrProgramCertifications where PSN = @PSN) PC on cc.CertificationId = pc.CertificationId ) as a on a.CertificationId = cc.CertificationId
where cc.CipCode = @CipCode
union
select pc.CertificationId as CertificationId, p.cipcode as CipCode, other as Credential,'' as LicensingCertificationProgram,
'' as IssuingOrganization, 'Yes' as ab
from (select * from enrProgramCertifications where CertificationId = '99999') pc join enrProgram p on pc.PSN = p.PSN
where p.CIPCode = @CipCode and p.PSN = @PSN
Union
select '99999' as CertificationId, @CipCode as CipCode, 'Other' as Credential,'' as LicensingCertificationProgram, '' as IssuingOrganization, '' as ab
order by ab desc, Credential
我的 linq 查询是
var t = ((from a in LkpCertifications
join d in EnrProgramCertifications on a.CertificationId equals d.CertificationId into ad
from d in ad.DefaultIfEmpty()
join c in EnrCertificationCipCodes on a.CertificationId equals c.CertificationId
where c.CipCode == "52.1999"
select new {
CertificationId = a.CertificationId,
CipCode = c.CipCode,
Credential = a.Credential,
Licensing = a.LicensingCertificationProgram,
IssuingOrganization = a.IssuingOrganization,
Psn = d.PSN != null ? d.PSN : 0,
ab = d.PSN != null ? "Yes" : ""
})
.Union (from k in EnrProgramCertifications
join l in LkpCertifications on k.CertificationId equals l.CertificationId where k.PSN == 19480
select new{
CertificationId = k.CertificationId,
CipCode = "52.1999",
Credential = k.Other,
Licensing = l.LicensingCertificationProgram,
IssuingOrganization = l.IssuingOrganization,
Psn = k.PSN != null ? k.PSN : 0,
ab = k.PSN != null ? "Yes" : ""
})).ToList();
t.Dump();
结果是我从左侧加入了更多行 table。如果我尝试通过使用 PSN 号码限定它来限制从左侧加入 table 的行,那么我不会从 lkpCertifications table 获得结果。 lkpCertifications 包含所有证书,而 enrProgramCertifications 仅包含那些已被选中的证书,就像订单和订单详细信息设置一样。
所以我目前的结果看起来像
我应该只得到一个 PSN 为 0 的项目 54 和一个 PSN 为 19480 的 55 记录。
对这个问题有什么想法吗?
我能够通过使用一对连接到对象中来解决这个问题,这些对象 defaultifempty() 到 return 左外连接。
certs = (from a in _context.LkpCertifications
join d in _context.EnrProgramCertifications on a.CertificationId equals d.CertificationId into ad
from d in ad.DefaultIfEmpty()
join c in _context.EnrCertificationCipCodes on a.CertificationId equals c.CertificationId into ac
from c in ac.DefaultIfEmpty()
where c.CipCode == CIP || d.Psn == Psn
select new vmManageCertifications
{
Psn = d.Psn == Psn ? d.Psn : 0,
Cipcode = c.CipCode == null ? "0" : c.CipCode,
CertificationId = a.CertificationId,
Credential = a.Credential == "Other" ? d.Other : a.Credential,
LicensingCertificationProgram = a.LicensingCertificationProgram,
IssuingOrganization = a.IssuingOrganization,
ProgramCertified = d.Psn == Psn ? true : false
}).ToList().GroupBy(i => i.CertificationId).Select(group => group.First()).OrderByDescending(i => i.ProgramCertified);