C# 实体框架中连接子句中的表达式之一的类型不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19184019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:20:11  来源:igfitidea点击:

The type of one of the expressions in the join clause is incorrect in Entity Framework

c#linqentity-frameworkjoinlinq-to-entities

提问by MaMu

While trying to execute this query:

在尝试执行此查询时:

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
            };

I'm getting this error:

我收到此错误:

The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.

join 子句中的表达式之一的类型不正确。调用“加入”时类型推断失败。

QOT_SEC_IDis of type decimaland PAY_SEC_IDis of type int32. I'm not allowed to change it in the table.

QOT_SEC_ID是 类型decimal并且 PAY_SEC_ID是 类型int32。我不允许在表中更改它。

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 }

but getting the error above.

但得到上面的错误。

采纳答案by Gert Arnold

The types andthe 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 }

or if p.PAY_SEC_IDwere an int?:

或者如果p.PAY_SEC_IDint?

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

回答by endouglas

In the original LINQ query, the where clause contains assignment, not comparison (i.e. need "==" instead of "=").

在原始 LINQ 查询中,where 子句包含赋值,而不是比较(即需要“==”而不是“=”)。

回答by usr

I'm guessing that one of the columns has a type that is implicitly convertable to the other. Likely, intand int?. That's why equalsimplicitly converts and new { X = 1 }is incompatible with new { X = (int?)1 }.

我猜其中一列的类型可以隐式转换为另一列。可能,int并且int?。这就是为什么equals隐式转换并且new { X = 1 }new { X = (int?)1 }.

Cast one of the conflicting columns to intor int?depending on whether nulls are possible or not. E.g.

将冲突的列之一投射到intint?取决于是否可能为空值。例如

new { Customer_ID = (int?)pl.Customer_ID, ... }

Admittedly, the compiler error in this particular case is quite unclear and does not point to the root cause.

诚然,这种特殊情况下的编译器错误很不清楚,也没有指出根本原因。

(This answer was rescued from a deleted duplicate. Since it's more complete than the currently accepted one I'll add it.)

(这个答案是从一个已删除的副本中拯救出来的。因为它比目前接受的更完整,我会添加它。)

回答by Simon Curtis

Hopefully this helps someone with a similar facepalm moment I just had, make sure the object's property names are the same. The error displays itself as:

希望这可以帮助我刚刚遇到类似面部表情的人,确保对象的属性名称相同。错误显示为:

The type of one of the expressions in the join clause is incorrect. Type inreference failed in the call to 'Join'

join 子句中的表达式之一的类型不正确。调用“加入”时类型引用失败

This is slightly misleading as this is the same message that appears when you have two value types that are different i.e. intand double.

这有点误导,因为这与当您有两个不同的值类型 ieint和时出现的消息相同double

What this actually meant in my case was that the two objects themselves were different types, not the values:

在我的情况下,这实际上意味着两个对象本身是不同的类型,而不是值:

join count in productCount on new { tool.ItemNo, tool.ItemType } equals new { count.OrigNumber, count.ItemType }

This was generating the following objects; which are obviously not comparable.

这正在生成以下对象;这显然没有可比性。

'a is new { int ItemNo, int ItemType }

'a is new { int ItemNo, int ItemType }

'a is new { int OrigNumber, int ItemType }

'a is new { int OrigNumber, int ItemType }

To correct this simply just name the OrigNumber field to ItemNo:

要更正此问题,只需将 OrigNumber 字段命名为 ItemNo:

join count in productCount on new { tool.ItemNo, tool.ItemType } equals new { ItemNo = count.OrigNumber, count.ItemType }