我正在尝试为具有多个条件的多个表加入但是对于其中一个联接我正在获得错误
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"
答案 0 :(得分:1)
我认为问题是必须将相等条件命名或具有相同的名称,这就是为什么你必须用这个替换有问题的行:
join prm in auditParameters on
new { KRAID = kra.KRAID, AuditID = qt.AuditParametersID }
equals
new { KRAID = prm.AuditKRAID, AuditID = prm.AuditingParametersID }
答案 1 :(得分:1)
问题是你加入了两个匿名类型,但这些类型不兼容 - 属性名称不同。你必须有一个密钥类型,所以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
的类型相同或者匿名类型仍然将不匹配。
答案 2 :(得分:0)
您的联接的相等条件不能具有匿名类型,您必须命名参数:
join prm in auditParameters on new
{
kraID = kra.KRAID, auditParameterID = qt.AuditParametersID
}
equals new
{
kraID = prm.AuditKRAID, auditParameterID = prm.AuditingParametersID
}
此外,参数的类型必须相同。 kra.KRAID
必须与prm.AuditKRAID
具有相同的类型。