无法从linq c#查询中的查询推断类型参数
下面是我的linq代码无法从linq c#查询中的查询推断类型参数,c#,linq,C#,Linq,下面是我的linq代码 mTimeslot = (from users in mContext.MFUsers join tblTime in mContext.tblTimeslots on users.UserID equals tblTime.varAdvisorId join tblApp in mContext.tblAppointments on new { varAdvisorId=users.UserID,tblTime.var
mTimeslot = (from users in mContext.MFUsers
join tblTime in mContext.tblTimeslots on users.UserID equals tblTime.varAdvisorId
join tblApp in mContext.tblAppointments on new { varAdvisorId=users.UserID,tblTime.varSlotId} equals new {tblApp.varAdvisorId, varSlotId = tblApp.varSlotId}
select new Timeslot
{
varAdvisorId = users.UserID,
varAdvisorName = users.varUserName
}).FirstOrDefault();
在第二个连接中,我得到一个错误,表示无法从查询中推断类型参数
tblTime.varSlotId
是一个整数,而tblApp.varSlotId
是一个可为空的整数。我可以看出错误在上面提到的列中。但是我无法将整数值转换为可为空的整数,因为这两列的名称都是varSlotId
tblTime.varSlotId是一个整数,其中tblApp.varSlotId是一个可为空的整数
这就是问题所在。你的两个匿名类型不一样,它们应该是一样的。可能最简单的方法就是强制转换tblTime.varSlotId
:
join tblApp in mContext.tblAppointments
on new { varAdvisorId = users.UserID, varSlotId = (int?) tblTime.varSlotId }
equals new { tblApp.varAdvisorId, tblApp.varSlotId }