无法将类型“System.Int32”强制转换为“System.Object”类型。 LINQ to Entities仅支持转换实体数据模型基元类型

时间:2011-04-06 18:49:41

标签: asp.net linq linq-to-entities primitive-types

我正在使用LINQ to Entities,我在运行时遇到以下错误。 “无法将类型'System.Int32'强制转换为'System.Object'.LINQ to Entities仅支持转换实体数据模型基元类型”。

LINQ语句如下:

(From item In _EntityObject.SystemUsers
 Where item.Username = UsernameValue And
     Not Equals(item.ID, IDValue)
 Select item.Active).Count

当IDValue设置为 Nothing 时,查询会完美执行,而当我将值设置为整数时,我收到上述错误。 item.ID 是Integer类型的属性,而 IDValue 的类型为 Nullable(Of Integer)

我有相同的LINQ查询,我在其他地方使用的不同字段正常工作。

(From item In _EntityObject.Languages
 Where item.Reference = Reference And
     Not Equals(item.ID, ID)
 Select item.Name).Count

2 个答案:

答案 0 :(得分:1)

只需使用=符号

即可
(From item In _EntityObject.SystemUsers
 Where item.Username = UsernameValue And
     Not (item.ID = IDValue)
 Select item.Active).Count

答案 1 :(得分:0)

问题通过以下声明解决:

(From item In _EntityObject.SystemUsers Where 
item.Username = Username And (Not (item.ID = IDValue)
 Or IDValue Is Nothing) Select item.Name).Count)