LINQ 在 bigint 上抛出无效的强制转换异常

发布于 2024-11-04 04:21:07 字数 1390 浏览 0 评论 0原文

我有一个看起来像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以可爱出名 2024-11-11 04:21:07

短语“物化值”是指从数据存储中检索的值。

可能发生的情况是,数据库将该列配置为 int,但在 EDMX 文件中它是 long(或 Int64)。

您放在前面的 (Int32) 转换(可能)被转换为数据存储(在 SQL Server 中,这意味着类似于 CAST([columnName] AS int)< /code>,因此,实体框架现在期望得到一个int而不是long

如果没有强制转换,它期望得到一个intlong 但得到 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 a long (or Int64).

The (Int32) cast you're putting on the front is (probably) being translated to the data store (in SQL Server, this means something like CAST([columnName] AS int), and consequently, the Entity Framework is now expecting to get an int instead of a long.

Without the cast, it's expecting a long but getting an int.

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)

橙味迷妹 2024-11-11 04:21:07

在我的存储过程中,我返回行号rowcount,我将其转换为int,现在它可以正常工作。

CAST (TotalCount AS INT)TotalCount

In my stored procedure, I was returning row number and rowcount, I casted it to int and it works properly now.

CAST (TotalCount AS INT)TotalCount
-柠檬树下少年和吉他 2024-11-11 04:21:07

该异常似乎是从实体框架引发的。您可能在 SSDL 文件中将列设置为 int 而不是 bigint

The exception seems to be thrown from the Entity Framework. You might have the column set as int instead of bigint in the SSDL file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文