Turning float or double into date within a lambda

-2

In the BD it has a field with a float value, which is actually the number of days from the 28/12/1800 date, which the Clarion programming language stores. Well, I have a REST service that is consumed by an Android App. What happens is that I would like to serialize this field, but already as date. If I do out of lambda it works (test), I just need to transforms inside the expression and I do not know how I do it: The command is?

var dt = new DateTime(1800, 12, 28).AddDays(79018).ToString("dd/MM/yyyy");

With this command dt = 02/05/2017 . But I need to load a property from my DTO and it gives an error that LINQ does not support ToString("dd/MM/yyyy"); My lambda is this:

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.FlagLiberacao == 1)
                        .Select(libera => new LiberacaoDTO
                        {
                            TipoVenda = libera.TipoVenda,
                            IdOrcamento = libera.IdOrcamento,
                            Juros = libera.Juros.ToString(),
                            Entrada = libera.Entrada.ToString(),
                            Acrescimo = libera.Acrescimo.ToString(),
                            Desconto = libera.Desconto.ToString(),
                            Mensagem = libera.Mensagem,
                            DataLib = new DateTime(1800,12,28).AddDays(libera.DataLib).ToString("dd/MM/yyyy"),//ERRO AQUI
                            Vencimento = libera.Vencimento.ToString(),
                            Vendedor = libera.Vendedor,
                            Cliente = libera.Cliente,
                            Filial = libera.Filial
                        }).ToList();
    
asked by anonymous 11.09.2017 / 17:27

1 answer

2

It is impossible because the Entity Framework does not know how to translate this to an SQL expression.

There are two very clear options:

1. Materialize the query data before you do the select

lista = contexto.Liberacoes
                .Where(a => a.FlagLiberacao == 1)
                .ToList() // <- A query é materializada para a memória aqui
                .Select(libera => new LiberacaoDTO
                {
                    // Demais campos
                    DataLib = new DateTime(1800,12,28).AddDays(libera.DataLib)
                                                      .ToString("dd/MM/yyyy")
                }).ToList();

2. Change the information out of select

lista = contexto.Liberacoes
            .Where(a => a.FlagLiberacao == 1)
            .ToList()
            .Select(libera => new LiberacaoDTO
            {
                // Demais campos
                DataLibOriginal = libera.DataLib,
            }).ToList();

lista.ForEach(e => e.DataLib = 
               new DateTime(1800,12,28).AddDays(e.DataLibOriginal).ToString("dd/MM/yyyy"));
    
11.09.2017 / 17:43