Return an object from two or more tables to the fields

Asked

Viewed 366 times

0

I have done some posts on Rest and Lambda, and to give a close on this subject(I hope), I have a question. A colleague here at the site recommended that I create a DTO and bring the database data based on this DTO class and not directly from Model. I did, fixed some bugs with help and solved. Well, it turns out that my service, for this situation, returns a DTO. How would I do this using two or more entities? So is my method today:

public List<LiberacaoDTO> getAutoriza(int idorcamento)
        {
            var lista = contexto.Liberacoes
                        .Where(lib => lib.IdOrcamento == idorcamento)
                        .Select(lib => new LiberacaoDTO
                        {
                            TipoVenda = lib.TipoVenda,
                            IdOrcamento = lib.IdOrcamento,
                            Juros = lib.Juros != 0 ? lib.Juros : 0,
                            MaxComi = lib.MaxComi,
                            Entrada = lib.Entrada != 0 ? lib.Entrada : 0,
                            Mensagem = lib.Mensagem,
                            Vendedor = lib.Vendedor,
                            Cliente = lib.Cliente,
                            Filial = lib.Filial
                        }).ToList();
            return lista;
        }

This is my DTO class

public LiberacaoDTO()
        {
            Mapper.Initialize(cfg =>
            {

                //string userName = null;
                cfg.CreateMap<LiberacaoDTO, Liberacao>()
                    .ForMember(d => d.Juros,
                        opt => opt.MapFrom(src => Juros.ToString("C2")
                        ));

                cfg.CreateMap<LiberacaoDTO, Liberacao>()
                .ForMember(e => e.Entrada,
                opt => opt.MapFrom(src => Entrada.ToString("C2")
                ));
            });
        }
        public int IdLiberacao { get; set; }
        public byte FlagLiberacao { 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; }
        [DefaultValue(0)]
        public float? HoraLib { 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 double Vencimento { get; set; }
        [DefaultValue(0)]
        public double Acrescimo { get; set; }
        [DefaultValue(0)]
        public float Entrada { get; set; }
        [DefaultValue(0)]
        public float Prazo { get; set; }
        [DefaultValue(0)]
        public float TotalLiquido { get; set; }
        [DefaultValue(0)]
        public float MinTotal { get; set; }
        public string Usuario { get; set; }
        [DefaultValue(0)]
        public decimal CustoDiario { get; set; }
        [DefaultValue(0)]
        public decimal MaxComi { get; set; }
        [DefaultValue(0)]
        public decimal ValorComi { get; set; }
        [DefaultValue(0)]
        public decimal NovaComi { get; set; }
        public string Mensagem { get; set; }
        public string MensagemRet { get; set; }
        [DefaultValue(0)]
        public double DataRetorno { get; set; }
        [DefaultValue(0)]
        public float HoraRetorno { get; set; }
        [DefaultValue(0)]
        public float TempoPrecesso { get; set; }
        public int Tipo { get; set; }
        public string Programa { get; set; }
        public string NomePc { get; set; }
        public string NomeProcedure { get; set; }
        [DefaultValue(0)]
        public decimal PercJurosTotal { get; set; }
        public byte FlagCulturaVencida { get; set; }
        public string Cultura { get; set; }
        public int CulturaVcto { get; set; }
        public byte FlagProrrogado { get; set; }
        [DefaultValue(0)]
        public float ValorProrrogado { get; set; }
        public int DiasAtrazo { get; set; }
        public int IdVendedor2 { get; set; }
        public string Vendedor2 { get; set; }
        [DefaultValue(0)]
        public float ComissaoVend2 { get; set; }
        public byte FlagCotacao { get; set; }
        public string TipoVenda1 { get; set; }
        public byte FlagReceberAtrazado { get; set; }
        public string AutorizouReceberAtrazado { get; set; }
    }

The point is that I need to include another class, which in my case is the ITENSLIB and I’ll have to create another DTO for her. That’s okay, but how would I put in the Lambda and what I return. I tried to return a object and the array or list is empty. How to include all this and return a single object populated with both tables in lambda. Below my service it did not work. The list came empty:

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

2 answers

1

You will need a second DTO which will be a property of the principal. After that just make a Join on LINQ to get popular data from the second table.

  • That I’m already doing, but my problem is what I return. But your last comment, it seems that can clarify me this difficulty I’m having.

1

The concept of DTO is to be a transfer class, you don’t necessarily need to add only the properties of a class to your DTO. It would be something like:

var lista = contexto.Liberacoes
    .Include(lib => lib.ITENSLIB)
    .Where(lib => lib.IdOrcamento == idorcamento)
    .Select(lib => new LiberacaoDTO
    {
        TipoVenda = lib.TipoVenda,
        ExemploItemLib = lib.ITENSLIB.Valor
    }).ToList();
return lista;

In this example I give a Include on all Itenslib relating to this Liberation. If your Itemlib has no relationship with Liberation, I recommend that you respect the principle of sole responsibility and do another method to consult and another DTO for it.

Browser other questions tagged

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