来自物化系统.Double'的指定演员表。输入' System.Single'类型无效。我能解决这个错误吗?

时间:2017-08-31 17:45:57

标签: c#

我的项目中有一个DTO课程。所以,当我在运行时运行时,我有这样的信息:

  

来自物化系统.Double'的指定演员表。输入到   ' System.Single'类型无效。

当我只放置float或double类型时会出现此消息。我映射了我的DTO类,但错误仍在继续。使用decimal或int工作正常。这是我的DTO课程:

public class LiberacaoItensDTO
    {
        public LiberacaoItensDTO()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                .ForMember(d => d.DataLib, opt => opt.MapFrom(src => Convert.ToDateTime(DataLib)));
                cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                .ForMember(d => d.Juros, opt => opt.MapFrom(src => Juros.ToString("C2")));
                cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                .ForMember(d => d.Desconto, opt => opt.MapFrom(src => Desconto.ToString("C2")));
                cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                .ForMember(d => d.Acrescimo, opt => opt.MapFrom(src => Acrescimo.ToString("C2")));
                cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
               .ForMember(d => d.Vencimento, opt => opt.MapFrom(src => Convert.ToDateTime(Vencimento)));
                cfg.CreateMap<LiberacaoItensDTO, Liberacao>()
                .ForMember(d => d.Entrada, opt => opt.MapFrom(src => Entrada.ToString("C2")));

                cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                .ForMember(d => d.Qtde, opt => opt.MapFrom(src => Qtde.ToString("C2")));
                cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                .ForMember(d => d.Unitario, opt => opt.MapFrom(src => Unitario.ToString("C2")));
                cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                .ForMember(d => d.Custo, opt => opt.MapFrom(src => Custo.ToString("C2")));
                cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                .ForMember(d => d.CustoDiario, opt => opt.MapFrom(src => CustoDiario.ToString("C2")));
                cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                .ForMember(d => d.UltCondicao, opt => opt.MapFrom(src => UltCondicao.ToString("C2")));
                cfg.CreateMap<LiberacaoItensDTO, ItensLib>()
                .ForMember(d => d.Total, opt => opt.MapFrom(src => Total.ToString("C2")));
            });
        }
        public int IdLiberacao { get; set; }
        [DefaultValue(0)]
        public int IdOrcamento { get; set; }
        [DefaultValue(0)]
        public int IdVendedor { get; set; }
        public string Vendedor { get; set; }
        public int IdFilial { get; set; }
        public string Filial { get; set; }
        [DefaultValue(0)]
        public float DataLib { get; set; }
        public int IdCliente { get; set; }
        public string Cliente { get; set; }
        public string TipoVenda { get; set; }
        [DefaultValue(0)]
        public float Juros { get; set; }
        [DefaultValue(0)]
        public float Desconto { get; set; }
        [DefaultValue(0)]
        public float Vencimento { get; set; }
        [DefaultValue(0)]
        public float Acrescimo { get; set; }
        [DefaultValue(0)]
        public float Entrada { get; set; }
        public string Mensagem { get; set; }
        public int IdProduto { get; set; }
        public string Produto { get; set; }
        [DefaultValue(0)]
        public float Qtde { get; set; }
        [DefaultValue(0)]
        public float Unitario { get; set; }
        [DefaultValue(0)]
        public float Custo { get; set; }
        [DefaultValue(0)]
        public float CustoDiario { get; set; }
        [DefaultValue(0)]
        public float UltCondicao { get; set; }
        [DefaultValue(0)]
        public float Total { get; set; }
    }

低于我的方法。此方法返回DTO对象类型:

public List<LiberacaoItensDTO> getAutoriza(int idorcamento)
        {

            var lista = contexto.Liberacoes
                        //.Where(lib => lib.IdOrcamento == idorcamento)
                        .Join(contexto.ItensLibs, lib => lib.IdOrcamento, itens => itens.IdOrcamento, (lib,itens) => new { lib, itens})
                        .Where(a => a.lib.IdOrcamento == a.itens.IdOrcamento && a.lib.IdOrcamento == idorcamento)
                        .Select(libera => new LiberacaoItensDTO
                        {
                            TipoVenda = libera.lib.TipoVenda,
                            IdOrcamento = libera.lib.IdOrcamento,
                            Juros = libera.lib.Juros != 0 ? libera.lib.Juros : 0,
                            //Entrada = libera.lib.Entrada != 0 ? libera.lib.Entrada : 0,
                            //Acrescimo = libera.lib.Acrescimo != 0 ? libera.lib.Acrescimo : 0,
                            //Desconto = libera.lib.Desconto != 0 ? libera.lib.Desconto : 0,
                            Mensagem = libera.lib.Mensagem,
                            //DataLib = libera.lib.DataLib != 0 ? libera.lib.DataLib : 0,
                            Qtde = libera.itens.Qtde != 0 ? libera.itens.Qtde : 0,
                            Vendedor = libera.lib.Vendedor,
                            Cliente = libera.lib.Cliente,
                            Filial = libera.lib.Filial
                        }).ToList();
            return lista;
        }

我的休息服务:

public class LiberacaoController : ApiController
    {
        AutorizadorContext contexto = new AutorizadorContext();
        PedidoLiberacao liberacao = new PedidoLiberacao();

        [AcceptVerbs("Get")]
        public IEnumerable<LiberacaoItensDTO> getLiberacao()
        {
            return liberacao.getAutoriza(1000012093).AsEnumerable().ToList();
        }
    }

如果我用float字段评论此行Juros = libera.lib.Juros != 0 ? libera.lib.Juros : 0,或其他行,请正常工作。否则不会。

3 个答案:

答案 0 :(得分:2)

我也遇到了这个错误,我在课堂上有浮动字段(比如你的DTO课程)

public class ReportArchiveHistoryQueryModel
{
    public DateTime ArchiveDate { get; set; }
    public float Energy { get; set; }
}

我有包含浮动列的sql表。当我通过存储过程获取一些数据时,

CREATE PROCEDURE [dbo].[sp_ReportArchiveHistory]
     @SmartCounterId VARCHAR(10)
    ,@StartDate SMALLDATETIME
    ,@EndDate SMALLDATETIME

AS
    SELECT ArchiveDate,Energy FROM Archive
    WHERE Archive.SmartCounterId=@SmartCounterId
    AND Archive.ArchiveDate>=@StartDate
    AND Archive.ArchiveDate<=@EndDate 

我在return result.ToList();

收到此错误
 public List<ReportArchiveHistoryQueryModel> ArchiveHistory(short smartCounterId, DateTime startDate,DateTime endDate)
    {
        var pSmartCounterId = new SqlParameter("SmartCounterId",smartCounterId);
        var pStartDate = new SqlParameter("StartDate",startDate);
        var pEndDate = new SqlParameter("EndDate ", endDate);

        var result = this.Database.SqlQuery<ReportArchiveHistoryQueryModel>("exec dbo.sp_ReportArchiveHistory @SmartCounterId, @StartDate, @EndDate", pSmartCounterId, pStartDate, pEndDate);

        return result.ToList();
    }

所以我解决了以下问题:

我认为C#不能将float列的数据转换为class的float字段。因此,我将存储过程中的浮点数据转换为varchar。

CREATE PROCEDURE [dbo].[sp_ReportArchiveHistory]
     @SmartCounterId VARCHAR(10)
    ,@StartDate SMALLDATETIME
    ,@EndDate SMALLDATETIME

AS
    SELECT ArchiveDate,CONVERT(varchar(10), Energy )as Energy FROM Archive
    WHERE Archive.SmartCounterId=@SmartCounterId
    AND Archive.ArchiveDate>=@StartDate
    AND Archive.ArchiveDate<=@EndDate 

用字符串更改float字段后。

我的查询模型:

 public class ReportArchiveHistoryQueryModel
{
    public DateTime ArchiveDate { get; set; }
    public string Energy { get; set; }
}

当我需要浮动之后,我再次解析浮动。

ReportArchiveHistoryBusinessModel reportArchiveHistoryBusinessModel=new ReportArchiveHistoryBusinessModel();
                    reportArchiveHistoryBusinessModel.ArchiveDate = item.ArchiveDate;
                    reportArchiveHistoryBusinessModel.Energy = float.Parse(item.Energy);

或第二个解决方案:

我刚将float字段改为double:)

 public class ReportArchiveHistoryQueryModel
{
    public DateTime ArchiveDate { get; set; }
    public double Energy { get; set; } 
}

答案 1 :(得分:1)

我解决了在我的DTO课程中将所有浮动和双重字段更改为字符串,并且在我的应用程序中,我更改为浮动并再次加倍。

答案 2 :(得分:1)

如果在数据库端数据类型包含浮点数,则在代码端加倍进行映射。