【问题标题】:The type arguments cannot be inferred from the query in linq c# query无法从 linq c# 查询中的查询推断类型参数
【发布时间】:2015-07-16 10:29:16
【问题描述】:

下面是我的一段 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();

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

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

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    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 }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      相关资源
      最近更新 更多