Return an object coming from two or more tables the fields

0

I have done some posts about rest and lambda, and to give an end to this subject (hopefully), I have a question. A colleague here on the site recommended that I create a DTO and bring the database data based on this DTO class and not directly from the 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 here's 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 issue is that I need to include another class, which in my case is ITENSLIB and I will have to create another DTO for it. That's ok, but how would I put it on Lambda and what do 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 filled with both tables in lambda. Below my service that did not work. The list came empty:

[AcceptVerbs("Get")]
        public IEnumerable<object> getLiberacao()
        {
            return liberacao.getAutoriza1(1000012093).AsEnumerable().ToList();
        }
    
asked by anonymous 27.08.2017 / 23:27

2 answers

1

You will need a second DTO that will be a primary property. After that, just do a join in LINQ to get popular data from the second table.

    
27.08.2017 / 23:47
1

The concept of the DTO is to be a transfer class, you do not necessarily need to add in your DTO just the properties of a class. It would look 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 an Include on all Itemsib relating to this Release. If your Itemlib does not have a Release relationship, I recommend that you respect the principle of single responsibility and make another method that makes the query and another DTO for it.

    
28.08.2017 / 00:48