简体   繁体   中英

The type arguments cannot be inferred from the query in linq c# query

Below is my piece of linq code

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();

In the second join I'm getting an error saying the type argument cannot be inferred from the query.

tblTime.varSlotId is an integer while tblApp.varSlotId is a nullable integer. I can figure out that the error is in the above mentioned column. But I'm unable to convert the integer value to a nullable integer as both the columns have the name of varSlotId .

tblTime.varSlotId is an integer where tblApp.varSlotId is a nullable integer.

Well that's the problem then. Your two anonymous types aren't the same, and they need to be. It's probably simplest just to cast tblTime.varSlotId :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM