无法从linq c#query中的查询推断出类型参数

Suv*_*tha 0 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.varSlotId} equals new {tblApp.varAdvisorId, varSlotId = tblApp.varSlotId}
             select new Timeslot
             {
                 varAdvisorId = users.UserID,
                 varAdvisorName = users.varUserName
             }).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

在第二个连接中,我收到一条错误,指出无法从查询中推断出类型参数.

tblTime.varSlotId是一个整数,tblApp.varSlotId而是一个可以为空的整数.我可以弄清楚错误是在上面提到的列中.但是我无法将整数值转换为可以为空的整数,因为两列的名称都是varSlotId.

Jon*_*eet 9

tblTime.varSlotId是一个整数,其中tblApp.varSlotId是一个可以为null的整数.

那么这就是问题所在.你的两个匿名类型是不一样的,他们需要.投射可能最简单tblTime.varSlotId:

join tblApp in mContext.tblAppointments
    on new { varAdvisorId = users.UserID, varSlotId = (int?) tblTime.varSlotId }
    equals new { tblApp.varAdvisorId, tblApp.varSlotId }
Run Code Online (Sandbox Code Playgroud)