调用多个连接条件时得到错误
本文关键字:错误 条件 连接 调用 | 更新日期: 2023-09-27 17:53:55
我正在尝试与多个条件的多个表连接,但对于一个连接,我得到错误
连接子句中一个表达式的类型不正确。调用"Join"时类型推断失败。
from aq in auditUnitQuestions
join wt in questionWt on aq.AuditQuestionWtID equals wt.AuditQuestionWtID
join qt in auditQuestions on wt.AuditQuestionID equals qt.AuditQuestionID
join ac in AuditComponents on qt.ComponentID equals ac.ComponentID
join kra in AuditKRAs on new { ac.ComponentID, qt.KRAID } equals new { kra.ComponentID, kra.KRAID }
//below I am getting error (The type of one of the expressions in the join clause
//is incorrect. Type inference failed in the call to 'Join'.
join prm in auditParameters on new { kra.KRAID, qt.AuditParametersID } equals new { prm.AuditKRAID, prm.AuditingParametersID }
where aq.Unitcode == calendar.UnitCode
&& wt.AuditTypeID == auditType
&& qt.Isactive == "Y"
问题是您正在连接两个匿名类型,但这些类型不兼容—属性名称不同。你必须有一个单一的密钥类型,所以equals
的两边必须是相同的类型,基本上。
而不是:
new { kra.KRAID, qt.AuditParametersID }
equals new { prm.AuditKRAID, prm.AuditingParametersID }
应该是这样的:
new { kra.KRAID, qt.AuditParametersID }
equals new { KRAID = prm.AuditKRAID, AuditParametersID = prm.AuditingParametersID }
或者您可以为两个匿名类型显式指定相同的名称:
new { x = kra.KRAID, y = qt.AuditParametersID }
equals new { x = prm.AuditKRAID, y = prm.AuditingParametersID }
当然,kra.KRAID
的类型必须与prm.AuditKRAID
的类型相同,同样,qt.AuditParametersID
的类型必须与prm.AuditingParametersID
的类型相同,否则匿名类型仍然不匹配。
我认为问题在于相等条件必须命名或具有相同的名称,这就是为什么您必须将有问题的行替换为:
join prm in auditParameters on
new { KRAID = kra.KRAID, AuditID = qt.AuditParametersID }
equals
new { KRAID = prm.AuditKRAID, AuditID = prm.AuditingParametersID }
您的连接的相等条件不能是匿名类型,您必须命名参数:
join prm in auditParameters on new
{
kraID = kra.KRAID, auditParameterID = qt.AuditParametersID
}
equals new
{
kraID = prm.AuditKRAID, auditParameterID = prm.AuditingParametersID
}
同时,参数的类型必须相等。kra.KRAID
必须与prm.AuditKRAID
具有相同的类型