Turn float or double into date within a lambda

Asked

Viewed 55 times

-2

In BD you have a field with a float value, which is actually the number of days from the date 28/12/1800, that the Clarion programming language stores. Well, I have a service REST 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 outside the lambda works(test), only I need to transform inside the expression and 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 error that the 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();
  • I do not know why the negativity, because the question is not broad. As I disagreed with two guys the other day, I feel that they always negatively negatively affect me, regardless of whether or not the question is within the criteria of the site. Mind girl those. People take comments to personal side always, disregarding the rules of the site.

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 doing 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 outside the 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"));

Browser other questions tagged

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