从实例化的"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:
从具体化的"System.Int32"类型到 "System.Double"类型无效.
The specified cast from a materialized 'System.Int32' type to the 'System.Double' type is not valid.
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();
WorkDay
,OnDutyDay
和Holiday
的数据类型为double
.这里没有Int32
,为什么我会出现此错误?
The datatype of WorkDay
, OnDutyDay
and Holiday
is double
. There is no Int32
here, so why do I get 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; }
}
推荐答案
我认为数据库表中各列的数据类型为Int32
,但是您的数据模型具有double
.您需要将数据模型的数据类型更改为int
. materialized
表示在数据库上运行查询时得到的类型.
I think the data type of the columns inside your database table is Int32
but your data model has double
. You need to change the data types of your data models to int
. By materialized
it means the type it got when it ran the query on the database.
这篇关于从实例化的"System.Int32"类型到"System.Double"类型的指定强制转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!