实体框架中连接子句中的表达式之一的类型不正确 [英] The type of one of the expressions in the join clause is incorrect in Entity Framework
问题描述
尝试执行此查询时:
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
};
我收到此错误:
join 子句中的表达式之一的类型不正确.调用加入"时类型推断失败.
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
类型.我不允许在表格中更改它.
QOT_SEC_ID
is of type decimal
and PAY_SEC_ID
is of type int32
.
I'm not allowed to change it in the table.
无论我做什么,我都无法在模型的属性中更改它.我试图转换这样的类型:
No matter what I do, I'm not able to change it in model's properties. I have tried to convert the types like this:
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 }
但得到上面的错误.
推荐答案
匿名类型中的类型和属性名称必须匹配:
The types and the names of the properties in the anonymous types must match:
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
,或者
...which will find no matches PAY_SEC_ID
is null
, or
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
).
...which defaults p1
to 0
when PAY_SEC_ID
is null
and again no match will be found (assuming that ID values will never be 0
).
这篇关于实体框架中连接子句中的表达式之一的类型不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!