在尝试执行此查询时:
var query = from dpr in ctx.DPR_MM
join q in ctx.QOT on dpr.DPR_QOT_ID equals qot_id
join p in ctx.PAY_MM on new { q.QOT_SEC_ID, dpr.DPR_TS } equals new { p.PAY_SEC_ID, p.PAY_DATE }
where q.QOT_ID = qot_id
select new
{
dpr.dpr_ts,
dpr.dpr_close,
pay.First().pay_dividend
};
我收到这个错误:
The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
QOT_SEC_ID
是decimal
类型,PAY_SEC_ID
是int32
类型。
我不允许在表中更改它。
无论我做什么,我都无法在模型的属性中更改它。 我试过像这样转换类型:
join p in ctx.PAY on new { sec_id = (Int32)(q.QOT_SEC_ID), dpr.DPR_TS } equals new { sec_id = (Int32)p.PAY_SEC_ID, p.PAY_DATE }
但是出现上面的错误。
最佳答案
匿名类型中的类型和属性名称必须匹配:
new { p1 = q.QOT_SEC_ID, p2 = dpr.DPR_TS }
equals
new { p1 = (decimal)p.PAY_SEC_ID, p2 = p.PAY_DATE }
或者如果 p.PAY_SEC_ID
是一个 int?
:
new { p1 = (int?)q.QOT_SEC_ID, p2 = dpr.DPR_TS }
equals
new { p1 = p.PAY_SEC_ID, p2 = p.PAY_DATE }
...这将找不到匹配项 PAY_SEC_ID
为 null
,或者
new { p1 = q.QOT_SEC_ID, p2 = dpr.DPR_TS }
equals
new { p1 = p.PAY_SEC_ID.GetValueOrDefault(), p2 = p.PAY_DATE }
...当 PAY_SEC_ID
为 null
时,默认 p1
为 0
并且再次找不到匹配项(假设 ID 值永远不会是 0
)。
关于c# - 连接子句中的一个表达式的类型在 Entity Framework 中不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19184019/