简体   繁体   English

从具体化的“System.Int32”类型到“System.Double”类型的指定转换无效

[英]The specified cast from a materialized 'System.Int32' type to the 'System.Double' type is not valid

When executing the following query, I get the error:执行以下查询时,出现错误:

The specified cast from a materialized 'System.Int32' type to the 'System.Double' type is not valid.从具体化的“System.Int32”类型到“System.Double”类型的指定转换无效。

var data = ctx.tblTO
                   .Where(m => m.Id == Id)
                   .GroupBy(m => m.EmployeeId)
                   .Select(m => new
                   {
                       workDay = m.Sum(k => k.WorkDay),
                       onDutyDay = m.Sum(k => k.OnDutyDay),
                       holiDay = m.Sum(k => k.Holiday)
                   })
                   .FirstOrDefault();

The datatype of WorkDay , OnDutyDay and Holiday is double . WorkDayOnDutyDayHoliday的数据类型是double There is no Int32 here, so why do I get this error?这里没有Int32 ,为什么会出现这个错误?

How can I solve this error?我该如何解决这个错误?

public class TO
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }           
    public int EmployeeId { get; set; }
    public double WorkDay { get; set; }
    public double OnDutyDay { get; set; }
    public double Holiday { get; set; }
}

I think the data type of the columns inside your database table is Int32 but your data model has double .我认为数据库表中列的数据类型是Int32但你的数据模型有double You need to change the data types of your data models to int .您需要将数据模型的数据类型更改为int By materialized it means the type it got when it ran the query on the database. materialized意味着它在数据库上运行查询时得到的类型。

First make sure your model and table column data type are same.首先确保您的模型和表列数据类型相同。

Try to change your query to this.尝试将您的查询更改为此。

var data = ctx.tblTO
           .Where(m => m.Id == Id)
           .GroupBy(m => m.EmployeeId)
           .Select(m => new
           {
               workDay = m.Select(k => k.WorkDay).DefaultIfEmpty(0).Sum(),
               onDutyDay = m.Select(k => k.OnDutyDay).DefaultIfEmpty(0).Sum(),
               holiDay = m.Select(k => k.Holiday).DefaultIfEmpty(0).Sum()
           })
           .FirstOrDefault();

If the collection is empty, it will return one element with the value of 0 and then the sum will be applied.如果集合为空,它将返回一个值为 0 的元素,然后应用总和。

For me I was calling a stored procedure from EF 6 of which I wrote a poco, but I didn't write the sproc.对我来说,我从 EF 6 调用了一个stored procedure ,其中我写了一个 poco,但我没有写 sproc。 I needed to look at all the tables that had field returning data types.我需要查看所有具有字段返回数据类型的表。 and then change my Poco from a int to boolean.然后将我的 Poco 从 int 更改为 boolean。

public Boolean Active { get; set; }

Apart from the other answers, I got another scenario where I faced the same error.除了其他答案之外,我还遇到了另一种情况,我遇到了同样的错误。 Which is, In our stored procedure a bigint was being casted to a decimal and on C# the same thing was being casted to int64.也就是说,在我们的存储过程中, bigint被转换为十进制,而在C# ,同样的事情被转换为 int64。 See:看:

SELECT Cast(@Request_Id as decimal) AS RetValue

What I did was changed the query to this:我所做的是将查询更改为:

SELECT @Request_Id AS RetValue

So, I removed the extra casting and the issue was gone.所以,我删除了额外的演员表,问题就消失了。 Somehow, it was maybe raising some sort of arithmetic exceptions but not quite sure yet.不知何故,它可能会引发某种算术异常,但还不太确定。 Will Update soon if I get to know the exact reason.如果我知道确切原因,将很快更新。

PS: My @Request_Id variable is of type bigint . PS:我的@Request_Id变量是bigint类型。

DECLARE @Request_Id bigint = 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从具体化的“System.Double”类型到“System.Int32”类型的指定转换无效但没有定义 Double - The specified cast from a materialized 'System.Double' type to the 'System.Int32' type is not valid But no Double defined 从物化的“System.Int32”类型到“System.Int64”类型的指定强制转换无效 - The specified cast from a materialized 'System.Int32' type to the 'System.Int64' type is not valid System.InvalidOperationException:从实例化的“ System.Int32”类型到可为空的“ Country”类型的指定强制转换无效 - System.InvalidOperationException: The specified cast from a materialized 'System.Int32' type to a nullable 'Country' type is not valid 从实例化的“ System.Guid”类型到“ System.Int32”类型的指定强制转换无效 - The specified cast from a materialized 'System.Guid' type to the 'System.Int32' type is not valid 从物化的“System.Int32”类型到“System.String”类型的指定强制转换无效 - The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid {“从物化'System.Guid'类型到'System.Int32'类型的指定强制转换无效。” - {“The specified cast from a materialized 'System.Guid' type to the 'System.Int32' type is not valid.” SQL和ASP.NET从实例化的“ System.Int64”类型到“ System.Int32”类型的指定强制转换无效 - SQL and ASP.NET The specified cast from a materialized 'System.Int64' type to the 'System.Int32' type is not valid 使用存储过程时,从实例化的“ System.Int32”类型到“ System.String”类型的指定强制转换无效 - The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid when using stored procedure System.InvalidCastException:无法将“System.Double”类型的 object 转换为代码中的“System.Int32”类型 - System.InvalidCastException: Unable to cast object of type 'System.Double' to type 'System.Int32' in code 从实例化的“ System.Double”类型到“ System.Single”类型的指定强制转换无效。 我可以解决这个错误吗 - The specified cast from a materialized 'System.Double' type to the 'System.Single' type is not valid. Can I solve this error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM