LINQ 在 bigint 上抛出无效的强制转换异常
我有一个看起来像这样的 LINQ 查询:
var clintLst = (from clntDt in ent.ClientDatas
where clntDt.CompanyName.Substring(0,searchWord.Length).Equals(searchWord, StringComparison.CurrentCultureIgnoreCase)
orderby clntDt.CompanyName
select new { ClientDataID = clntDt.ClientDataID,
CompanyName = clntDt.CompanyName,
ContactName = (clntDt.ContactFirstName + " " + clntDt.ContactLastName),
CompanyLocation = clntDt.Location.LocationCity.CityName + ", " + clntDt.Location.LocationState.StateCode
} ).Distinct().Take(10);
但是,它抛出以下异常:
来自物化的指定转换 'System.Int32' 类型到 “System.Int64”类型无效。 [..] 异常详细信息: System.InvalidOperationException: 来自物化的指定转换 'System.Int32' 类型到 “System.Int64”类型无效。
源文件: C:\TempPersonalCode\TransportTracking\TransportTracking\TransportTracking\Controllers\AJAXController.cs 线路:35
(第 35 行是 select 子句)
我很困惑,因为如果将其更改为:
select new { ClientDataID = clntDt.ClientDataID,
CompanyName = clntDt.CompanyName,
那么
select new { ClientDataID = (Int32)clntDt.ClientDataID,
CompanyName = clntDt.CompanyName,
它就可以正常工作。匿名对象不应该使用反射来确定它的类型吗?如果是这样,为什么它决定它是“Int32”而不是 long ?在 EDMX 中,我将其作为 Int64。
I have a LINQ query that looks something like this:
var clintLst = (from clntDt in ent.ClientDatas
where clntDt.CompanyName.Substring(0,searchWord.Length).Equals(searchWord, StringComparison.CurrentCultureIgnoreCase)
orderby clntDt.CompanyName
select new { ClientDataID = clntDt.ClientDataID,
CompanyName = clntDt.CompanyName,
ContactName = (clntDt.ContactFirstName + " " + clntDt.ContactLastName),
CompanyLocation = clntDt.Location.LocationCity.CityName + ", " + clntDt.Location.LocationState.StateCode
} ).Distinct().Take(10);
However, it is throwing the following exception:
The specified cast from a materialized
'System.Int32' type to the
'System.Int64' type is not valid. [..]
Exception Details:
System.InvalidOperationException: The
specified cast from a materialized
'System.Int32' type to the
'System.Int64' type is not valid.Source File:
C:\TempPersonalCode\TransportTracking\TransportTracking\TransportTracking\Controllers\AJAXController.cs
Line: 35
(Line 35 is the select clause)
I'm confused because if change:
select new { ClientDataID = clntDt.ClientDataID,
CompanyName = clntDt.CompanyName,
to
select new { ClientDataID = (Int32)clntDt.ClientDataID,
CompanyName = clntDt.CompanyName,
then it works fine. Isn't an anonymous object supposed to use reflection to determine it's type? if so, why is it deciding that it's an "Int32" instead of a long? Within the EDMX I have it as an Int64.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
短语“物化值”是指从数据存储中检索的值。
可能发生的情况是,数据库将该列配置为
int
,但在 EDMX 文件中它是long
(或Int64
)。您放在前面的
(Int32)
转换(可能)被转换为数据存储(在 SQL Server 中,这意味着类似于CAST([columnName] AS int)< /code>,因此,实体框架现在期望得到一个
int
而不是long
,如果没有强制转换,它期望得到一个
int
。long
但得到int
解决方案是更改 EDMX 文件或更改列,以便 EDMX 文件中的数据类型与数据库中的数据类型匹配。 .
(乔特)
The phrase "materialized value" refers to the value that was retrieved from the data store.
What's probably happening is that the database has that column configured as an
int
, but in your EDMX file it's along
(orInt64
).The
(Int32)
cast you're putting on the front is (probably) being translated to the data store (in SQL Server, this means something likeCAST([columnName] AS int)
, and consequently, the Entity Framework is now expecting to get anint
instead of along
.Without the cast, it's expecting a
long
but getting anint
.The solution is to either change the EDMX file or change the column, so that the data type in the EDMX file matches the data type in the database.
(jhott)
在我的存储过程中,我返回
行号
和rowcount
,我将其转换为int
,现在它可以正常工作。In my stored procedure, I was returning
row number
androwcount
, I casted it toint
and it works properly now.该异常似乎是从实体框架引发的。您可能在 SSDL 文件中将列设置为
int
而不是bigint
。The exception seems to be thrown from the Entity Framework. You might have the column set as
int
instead ofbigint
in the SSDL file.