c# - 连接子句中的一个表达式的类型在 Entity Framework 中不正确

标签 c# linq entity-framework join linq-to-entities

在尝试执行此查询时:

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_IDdecimal 类型,PAY_SEC_IDint32 类型。 我不允许在表中更改它。

无论我做什么,我都无法在模型的属性中更改它。 我试过像这样转换类型:

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_IDnull,或者

new { p1 = q.QOT_SEC_ID, p2 = dpr.DPR_TS } 
    equals 
new { p1 = p.PAY_SEC_ID.GetValueOrDefault(), p2 = p.PAY_DATE }

...当 PAY_SEC_IDnull 时,默认 p10 并且再次找不到匹配项(假设 ID 值永远不会是 0)。

关于c# - 连接子句中的一个表达式的类型在 Entity Framework 中不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19184019/

相关文章:

c# - 使用 LINQ,如何将 IList<IList<object>> 转换为 IList<object>?

c# - Entity Framework ICollection 导航属性仅在测试时为 null

c# - 在 EF CF 中建模有向图

c# - Twitter OAuth 请求 token 返回未授权

c# - struct tostring() 方法重定向到抽象类 ValueType

c# - 如何读取 XML 以创建 resx 文件

c# - 无法使用 Lambda 的扩展方法访问实例变量

c# - Entity Framework 程序集中的重复类型名称 (6.1.0)

c# - 使用 C# MVC4 并将 DbContext 类名称与 ConnectionString 名称匹配以使用正确的数据库?

c# - 在 C# 中检查代码重复的工具