I am trying to join for multiple tables with multiple conditions but for 1 of the joins 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'.
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"
The problem is that you're joining over two anonymous types, but those types aren't compatible - the property names are different. You've got to have a single key type, so both sides of the equals
must be of the same type, basically.
Instead of this:
new { kra.KRAID, qt.AuditParametersID }
equals new { prm.AuditKRAID, prm.AuditingParametersID }
You should have something like:
new { kra.KRAID, qt.AuditParametersID }
equals new { KRAID = prm.AuditKRAID, AuditParametersID = prm.AuditingParametersID }
Or you could just explicitly specify the same names for both anonymous types:
new { x = kra.KRAID, y = qt.AuditParametersID }
equals new { x = prm.AuditKRAID, y = prm.AuditingParametersID }
Of course, the type of kra.KRAID
must be the same as the type of prm.AuditKRAID
, and likewise the type of qt.AuditParametersID
must be the same as the type of prm.AuditingParametersID
or the anonymous types still won't match.
See more on this question at Stackoverflow