Error with lambda and fields float

Asked

Viewed 48 times

1

Every time I carry my lambda, I make that mistake:

System.Invalidoperationexception: 'The specified cast from a materialized 'System.Double' type to the 'System.Single' type is not Valid.'

This only happens with float fields. If I comment the fields and leave only the string fields, it runs normally. See the class that implements the lambda:

public class Autoriza
    {
        InetContext contexto = new InetContext();
        Liberacao liberacao = new Liberacao();
        public IEnumerable<object> getAutoriza()
        {
            var lista = contexto.Liberacoes
                .Join(contexto.ItensLibs, lib => lib.IdOrcamento, itens => itens.IdOrcamento, (lib, itens) => new { lib, itens })
                .Where(a => a.lib.IdOrcamento == a.itens.IdOrcamento)
                .Select(libera => new
                {
                    libera.lib.TipoVenda,
                    libera.lib.Vencimento,
                    libera.lib.Juros,
                    libera.lib.Entrada,
                    libera.lib.Acrescimo,
                    libera.lib.Desconto,
                    libera.lib.Mensagem,
                    libera.lib.DataLib,
                    libera.lib.Vendedor,
                    libera.lib.Cliente,
                    libera.lib.Filial,
                    libera.itens.Produto,
                    libera.itens.Qtde,
                    libera.itens.Unitario,
                    libera.itens.Custo,
                    libera.itens.CustoDiario,
                    libera.itens.UltCondicao,
                    libera.itens.Total
                }).ToList();

            return lista;
        }
    }

This is my Liberation domain class:

[Table("LIBERACAO")]
    public class Liberacao
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("ID_LIBERACAO")]
        public int IdLiberacao { get; set; }
        [Column("FLAG_LIBERACAO")]
        public byte FlagLiberacao { get; set; }
        [Column("ID_ORCAMENTO")]
        public int IdOrcamento { get; set; }
        [Column("ID_VENDEDOR")]
        public int IdVendedor { get; set; }
        [Column("VENDEDOR")]
        public string Vendedor { get; set; }
        [Column("ID_FILIAL")]
        public int IdFilial { get; set; }
        [Column("FILIAL")]
        public string Filial { get; set; }
        [Column("DATALIB")]
        public float DataLib { get; set; }
        [Column("HORALIB")]
        public float HoraLib { get; set; }
        [Column("ID_CLIENTE")]
        public int IdCliente { get; set; }
        [Column("CLIENTE")]
        public string Cliente { get; set; }
        [Column("TIPO_VENDA")]
        public string TipoVenda { get; set; }
        [Column("JUROS")]
        public float Juros { get; set; }
        [Column("DESCONTO")]
        public float Desconto { get; set; }
        [Column("VENCIMENTO")]
        public float Vencimento { get; set; }
        [Column("ACRESCIMO")]
        public float Acrescimo { get; set; }
        [Column("ENTRADA")]
        public float Entrada { get; set; }
        [Column("PRAZO")]
        public float Prazo { get; set; }
        [Column("TOTAL_LIQUIDO")]
        public float TotalLiquido { get; set; }
        [Column("MIN_TOTAL")]
        public float MinTotal { get; set; }
        [Column("USUARIO")]
        public string Usuario { get; set; }
        [Column("CUSTODIARIO")]
        public decimal CustoDiario { get; set; }
        [Column("MAX_COMI")]
        public decimal MaxComi { get; set; }
        [Column("VALOR_COMI")]
        public decimal ValorComi { get; set; }
        [Column("NOVA_COMI")]
        public decimal NovaComi { get; set; }
        [Column("MENSSAGEM")]
        public string Mensagem { get; set; }
        [Column("Menssagem_RET")]
        public string MensagemRet { get; set; }
        [Column("DataRetorno")]
        public decimal DataRetorno { get; set; }
        [Column("HoraRetorno")]
        public decimal HoraRetorno { get; set; }
        [Column("TempoProcesso")]
        public decimal TempoPrecesso { get; set; }
        [Column("Tipo")]
        public int Tipo { get; set; }
        [Column("PROGRAMA")]
        public string Programa { get; set; }
        [Column("NOME_PC")]
        public string NomePc { get; set; }
        [Column("NOME_PROCEDURE")]
        public string NomeProcedure { get; set; }
        [Column("Perc_Juros_Total")]
        public decimal PercJurosTotal { get; set; }
        [Column("FLAG_CULTURAVENCIDA")]
        public byte FlagCulturaVencida { get; set; }
        [Column("CULTURA")]
        public string Cultura { get; set; }
        [Column("CULTURA_VCTO")]
        public int CulturaVcto { get; set; }
        [Column("FLAG_PRORROGADO")]
        public byte FlagProrrogado { get; set; }
        [Column("VALOR_PRORROGADO")]
        public decimal ValorProrrogado { get; set; }
        [Column("DIAS_ATRASO")]
        public int DiasAtrazo { get; set; }
        [Column("ID_VENDEDOR2")]
        public int IdVendedor2 { get; set; }
        [Column("VENDEDOR2")]
        public string Vendedor2 { get; set; }
        [Column("COMISSAO_VEND2")]
        public decimal ComissaoVend2 { get; set; }
        [Column("FLAG_COTACAO")]
        public byte FlagCotacao { get; set; }
        [Column("TipoVenda")]
        public string TipoVenda1 { get; set; }
        [Column("Flag_Receber_Atrasado")]
        public byte FlagReceberAtrazado { get; set; }
        [Column("Autorizou_Receber_Atrasado")]
        public string AutorizouReceberAtrazado { get; set; }

    }

and the ITENSLIB domain class

[Table("ITENSLIB")]
    public class ItensLib
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("ID_ITENSLIB")]
        public int IdItensLib { get; set; }
        [Column("ID_ORCAMENTO")]
        public int IdOrcamento { get; set; }
        [Column("ID_PRODUTO")]        
        public int IdProduto { get; set; }
        [Column("PRODUTO")]
        public string Produto { get; set; }
        [Column("QTDE")]
        public float Qtde { get; set; }
        [Column("UNITARIO")]
        public float Unitario { get; set; }
        [Column("CUSTO")]
        public float Custo { get; set; }
        [Column("MINIMO")]
        public float Minimo { get; set; }
        [Column("TOTAL")]
        public float Total { get; set; }
        [Column("CUSTODIARIO")]
        public decimal CustoDiario { get; set; }
        [Column("FABRICANTE")]
        public string Fabricante { get; set; }
        [Column("ULT_CONDICAO")]
        public decimal UltCondicao { get; set; }
        [Column("PROGRAMA")]
        public string Programa { get; set; }
        [Column("NOME_PC")]
        public string NomePc { get; set; }
        [Column("NOME_PROCEDURE")]
        public string NomeProcedure { get; set; }
        [Column("Flag_Vencido")]
        public byte FlagVencido { get; set; }
        [Column("TipoVenda")]
        public string TipoVenda { get; set; }
        [Column("VENDA_VISTA")]
        public float VendaVista { get; set; }
        [Column("MARGEM_AVISTA")]
        public float MargemAvista { get; set; }
        [Column("QTDE_NEG_AVISTA")]
        public float QtdNegAvista { get; set; }
        [Column("VENDA_PRAZO")]
        public float VendaPrazo { get; set; }
        [Column("PM_PRAZO")]
        public float PmPrazo { get; set; }
        [Column("MARGEM_PRAZO")]
        public float MargemPrazo { get; set; }
        [Column("JUROS_PRAZO")]
        public float JurosPrazo { get; set; }
        [Column("QTDE_PRAZO")]
        public float QtdePrazo { get; set; }
        [Column("VENDA_VISTA_ANT")]
        public float VendaVistaAnt { get; set; }
        [Column("MARGEM_AVISTA_ANT")]
        public float MargemAvistaAnt { get; set; }
        [Column("QTDE_NEG_AVISTA_ANT")]
        public float QtdeNegAvistaAnt { get; set; }
        [Column("VENDA_PRAZO_ANT")]
        public float VendaPrazoAnt { get; set; }
        [Column("PM_PRAZO_ANT")]
        public float PmPrazoAnt { get; set; }
        [Column("MARGEM_PRAZO_ANT")]
        public float MargemPrazoAnt { get; set; }
        [Column("JUROS_PRAZO_ANT")]
        public float JurosPrazoAnt { get; set; }
        [Column("QTDE_PRAZO_ANT")]
        public float QtdePrazoAnt { get; set; }
        [Column("VENDA_VISTA_ANT1")]
        public float VendaVistaAnt1 { get; set; }
        [Column("MARGEM_AVISTA_ANT1")]
        public float MargemAvistaAnt1 { get; set; }
        [Column("QTDE_NEG_AVISTA_ANT1")]
        public float QtdeNegAvistaAnt1 { get; set; }
        [Column("VENDA_PRAZO_ANT1")]
        public float VendaPrazoAnt1 { get; set; }
        [Column("PM_PRAZO_ANT1")]
        public float PmPrazoAnt1 { get; set; }
        [Column("MARGEM_PRAZO_ANT1")]
        public float MargemPrazoAnt1 { get; set; }
        [Column("JUROS_PRAZO_ANT1")]
        public float JurosPrazoAnt1 { get; set; }
        [Column("QTDE_PRAZO_ANT1")]
        public float QtdePrazoAnt1 { get; set; }
    }

And my controller, where I call my m,Del that runs the lambda

 public class AutorizaController : ApiController
    {
        Autoriza autoriza = new Autoriza();

        [AcceptVerbs("Get")]
        public IEnumerable<object> getLiberacao()
        {
            return autoriza.getAutoriza().ToList();
        }
    }

This is the Release table with the fields coming in the model. I put only Liberation, because I made a test only with her fields and continues with the same mistake. inserir a descrição da imagem aqui

  • 1

    Where the mistake happens?

  • @LINQ, I forgot to put the controller that calls, but I already edited the post and put the controller code.

  • What are the column types (where in the system it is float) in the database?

  • Yes, I put the same types of fields, except for the REAL fields in the bank, which in my class I changed to decimal.

1 answer

1

The fields of the type REAL cannot be represented by decimal, they are floating point numbers and therefore must be represented by a field double or even float.

I based my answer on what was said in the comments (transcript below - highlighted by me) because the question did not have this information.

"(...) I put the same kinds of fields, except for the REAL fields in the bank, which in my class I changed to decimal."

  • Ok, I did not put this, because for this context, no decimal field is being called, as I said I commented all the fields of the Itenslib table/class, it has a REAL field in the database, but I commented to make this test and the error persists.

  • @pnet Whether or not you "call" the field should fail. If you try to make a simple contexto.Liberacoes.ToList() You must have made a mistake.

  • Okay, I’m correcting that. It’s just that I caught my VS and I’m already going to test it. I’m throwing it all to double.

  • I made the changes in the poco class, replacing in the bank what is REAL for the DOUBLE type in the class. I did that and even then, the error continues. I will comment, leave the strings fields and go putting field to field

  • I noticed that with Int fields does not give the problem, only when I load fields float, double or decimal.

  • that line on the lambda libera.itens.UltCondicao in the bank is REAL, if I put double or float gives the error, but if I put Double, then it works, but if I do this with others, the error continues.

  • You’re making this mistake now: The entity or complex type AutorizadorService.Infra.Data.Context.Liberacao' cannot be constructed in a LINQ to Entities query

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.