实体类:

    public class ReportEntity
{
public string FactorName { get; set; }
public double MaxVal { get; set; }
public double MinVal { get; set; }
public double AvgVal { get; set; }
public string UpdateTime { get; set; }
public string Unit { get; set; }
public int DecimalDigit { get; set; }
}

SQL语句

            string sqlstr = "   select b.FactorName,cast(MAX(a.Val) as float) as MaxVal,cast(MIN(a.Val) as float) as MinVal,cast(AVG(a.Val) as float) as AvgVal,"
+ strTime + " as UpdateTime,b.Unit,b.DecimalDigit "
+ " from Instrument_Data a,Factor b "
+ " where a.UpdateTime between '" + dtBegin + "' and '" + dtEnd + "' "
+ " and a.FactorID = b.FactorID "
+ " group by b.FactorName,b.Unit, " + strTime + ",b.DecimalDigit "
+ " order by b.FactorName,b.Unit," + strTime + ",b.DecimalDigit "; using (var db = new DAL.WSIPCContext())
{
var result3 = db.Database.SqlQuery<Entity.ReportEntity>(sqlstr);
this.lsvData.Items.Clear();
foreach (Entity.ReportEntity i in result3)
{
ListViewItem item = new ListViewItem();
item.SubItems.Clear();
item.Text = i.UpdateTime;
item.SubItems.Add(i.FactorName);
string format = "#0.".PadRight(i.DecimalDigit + 3, '0');
item.SubItems.Add(i.MaxVal.TryToDoubleStr(format));
item.SubItems.Add(i.MinVal.TryToDoubleStr(format));
item.SubItems.Add(i.AvgVal.TryToDoubleStr(format));
item.SubItems.Add(i.Unit);
this.lsvData.Items.Add(item);
}
}

将double换成float出现错误。EF使用System.Double对应SQLServer中的float。

重点关注:此处对SQL语句的结果也有要求,结果必须转换为float,才能与实体类映射成功!

EntityFramework异常The specified cast from a materialized 'System.Double' type to the 'System.Single' type is not valid.的更多相关文章

  1. entity framework异常 The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid

    ROW_NUMBER() OVER (ORDER BY (select Null)) AS Id entity framework 查询中有这句会有异常

  2. 异常:unity3d ArgumentException: The Assembly System.Configuration is referenced by System.Data.

    异常:ArgumentException: The Assembly System.Configuration is referenced by System.Data. But the dll is ...

  3. 异常:System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)

    异常:System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid) 原因:该引用所需.NET Framework版本为4.5 ...

  4. Java-maven异常-cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突

    使用maven开发web应用程序, 启动的时候报错: jar not loaded. See Servlet Spec . Offending class: javax/servlet/Servlet ...

  5. EntityFramework 异常 -- An entity object cannot be referenced by multiple instances of IEntityChangeTracker

    问题      在调用 DbSet 的 Attach()  方法时(与将 Entity 设置为 EntityState.Unchanged 状态等价)报告以下错误:      An entity ob ...

  6. 异常: Bean named 'org.springframework.transaction.interceptor.TransactionInterceptor#0' is expected to be of type 'org.aopalliance.aop.Advice' but was actually of type 'org.springframework.transaction.i

    场景: 在使用spring整合hibernate事务时报错解决: spring-aop中已经包含aopaliance,删除多余的jar包

  7. Unable to cast COM object of type 'Shell32.ShellClass' to interface type 'Shell32.IShellDispatch6'.

    VS 里面的Interop.Shell32.dll(1.0) 这个版本太低了,需要高版本的(我使用的是1.2.107.0) 具体下载地址  自己找吧

  8. EF添加Msysql实体异常:表“TableDetails”中列“IsPrimaryKey”的值为 DBNull。 ---> System.InvalidCastException: 指定的转换无效。

    尝试一下以下步骤: 1.关闭VS项目, 以管理员权限来打开: 1.执行语句 set global optimizer_switch='derived_merge=OFF'; set optimizer ...

  9. ssm裤架搭建异常: Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' ...

随机推荐

  1. JAVA 对基础知识的加强

    一.我们在写类的时候.设置字段的时候含义: 简单的javabean类: package jd.com.vo; import jd.com.mybaitstest.account; public cla ...

  2. visual studio 插件

    一:VS10x Code Map(打开后台cs文件,插件列出了所有的字段.方法,方便当代码行多的时候查找,提升工作效率) 1:下载 https://marketplace.visualstudio.c ...

  3. Android 将系统的back键模拟成为home键的功能

    @Override public void onBackPressed() { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFl ...

  4. 牛客练习赛44 B题 (思维)

    链接:https://ac.nowcoder.com/acm/contest/634/B 来源:牛客网 给出n条线段,第i条线段的长度为ai, 每次可以从第i条线段的j位置跳到第i + 1条线段的j+ ...

  5. WPF编程,通过Double Animation同时动态缩放和旋转控件的一种方法。

    原文:WPF编程,通过Double Animation同时动态缩放和旋转控件的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_4330793 ...

  6. Android开发——Fragment知识整理(二)

    0.  前言 Android开发中的Fragment的应用非常广泛,在Android开发--Fragment知识整理(一)中简单介绍了关于Fragment的生命周期,常用API,回退栈的应用等知识.这 ...

  7. MySQL优化:explain using temporary

    什么时候会使用临时表:group/order没设计好的时候 1.order没用索引 2.order用了索引, 但不是和where相同的索引 3.order用了两个索引, 但不是联合索引 4.order ...

  8. mybatis源码-解析配置文件(一)之XML的DOM解析方式

    目录 简介 Java 中 XML 文件解析 解析方式 DOM 解析 XML 新建 XML 文件 DOM 操作相关类 Java 读取 XML 文件 一起学 mybatis @ 简介 在之前的文章< ...

  9. vuex实践之路——笔记本应用(三)

    Actions Action 类似于 mutation,不同在于: Action 提交的是 mutation,而不是直接变更状态. Action 可以包含任意异步操作. 让我们来注册一个简单的 act ...

  10. 【DDD】业务建模实践 —— 发布帖子

    本文是基于上一篇‘业务建模战术’的实践,主要讲解‘发表帖子’场景的业务建模,包括:业务建模.业务模型.示例代码:示例代码会使用java编写,文末附有github地址.相比于<领域驱动设计> ...