How to convert Float to String using LINQ to entity?

1

How to convert Float to String using LINQ to entity?

I need to convert a field from my database that has the float type to the string type in my C # using LINQ.

Try using Convert.ToString,

 ChaveArquivo = Convert.ToString(n.scanIma.Imagem), 

but this generates the following query in SQL.

CONVERT(NVARCHAR(30), [t0].[Imagem], 2)

Returning the 8.12500000000000000e + 003 value, with the original value being 8125 , I want to convert to string with the same value "8125" .

This can be done right in the database with the Str( [t0].[Imagem]) function.

I need an equivalent in LINQ.

My query;

using (var dm = DataContextFactory.GetContext(pacoteUsuario.Usuario))
{
    var consulta = (from docsCol in dm.Tabela_DocsColetivas
                    join doc in dm.Tabela_Documentos on docsCol.Apol_coletiva equals doc.Apol_coletiva
                    join scanIma in dm.Tabela_ScanImagens on new { Documento = doc.Documento, Alteracao = doc.Alteracao } equals new { Documento = scanIma.Documento.GetValueOrDefault(), Alteracao = scanIma.Alteracao.GetValueOrDefault() }
                    join tip in dm.Tabela_TiposImagens on scanIma.Cod_tipoimg equals tip.Cod_tipoimg into ti
                    from tipImag in ti.DefaultIfEmpty()
                    join ProdSubs in dm.Tabela_ProdutosSubs on doc.Cod_sub equals ProdSubs.Cod_sub into ps
                    from ProdSub in ps.DefaultIfEmpty()
                    select new
                    {
                        scanIma,
                        doc,
                        tipImag,
                        docsCol,
                        ProdSub,
                    })
                    .AsQueryable();

    var arquivos = consulta
        .Select(n => new ListaArquivos
        {
            NumeroContrato = n.docsCol.Apolice,
            NumeroApolice = n.doc.Apolice,
            NumeroFatura = n.doc.Endosso,
            CodSubGrupo = n.ProdSub.Cod_sub,
            NomeSubGrupo = n.ProdSub.Descricao,
            Competencia = String.Concat(n.doc.Mes_producao, "/", n.doc.Ano_producao),
            DescrArquivo = n.scanIma.Descricao,
            TipoImagem = Convert.ToDouble(n.tipImag.Tipo_uso),
            DescrTipoImagem = n.tipImag.Descricao == null ? string.Empty : n.tipImag.Descricao,
            ChaveArquivo = Convert.ToString(n.scanIma.Imagem),
        });

    this.listaArquivos.AddRange(arquivos.ToList());
    listArquivos.Add(this);
    return listArquivos;
}

Class File List

namespace SistemasSeguros.COL.View.Webservice.Parâmetros
{
    public class ListaArquivos
    {
        public string NumeroContrato { get; set; }
        public string NumeroApolice { get; set; }
        public string NumeroFatura { get; set; }
        public double? CodSubGrupo { get; set; }
        public string NomeSubGrupo { get; set; }
        public string Competencia { get; set; }
        public string DescrArquivo { get; set; }
        public double? TipoImagem { get; set; }
        public string DescrTipoImagem { get; set; }
        public string ChaveArquivo { get; set; }
    }
}
    
asked by anonymous 22.09.2016 / 15:20

2 answers

12

You can use SqlFunctions.StringConvert() :

ChaveArquivo = StringConvert(n.scanIma.Imagem)

This answers the question directly. But it does not solve the problem shown in the comments.

The problem is that type float has no ability to give exactness. I've already answered on several questions here . The problem can not be solved any other way. And the worst is that it seems to solve in some cases. This is a danger because it causes the sense of security that does not exist. Of course it might just be used in situations that gambiarras solve. But it is not possible to solve all cases. If the exact information is not available you can not guarantee that it will get it, no matter what algorithm you use.

    
04.10.2016 / 02:02
1

I was able to solve the problem using two conversions one for ToInt64 and one for ToString.

var arquivos = consulta
            .Select(n => new ListaArquivos
            {
                NumeroContrato = n.docsCol.Apolice,
                NumeroApolice = n.doc.Apolice,
                CodSubGrupo = n.ProdutosSub.Cod_sub,
                NomeSubGrupo = n.ProdutosSub.Descricao,
                Competencia = n.doc.Mes_producao + '/' + n.doc.Ano_producao,
                DescrArquivo = n.scanIma.Descricao,
                TipoImagem = Convert.ToDouble(n.tipoImag.Tipo_uso),
                DescrTipoImagem = n.tipoImag.Descricao,
                ChaveArquivo = Convert.ToString(Convert.ToInt64(n.scanIma.Imagem)),
            });
    
22.09.2016 / 17:44