How to concatenate fields in the Line and rename

Asked

Viewed 619 times

1

I made a Burst and it is not working concatenation and renaming it. See:

var monta_arvore = (from rup in db.Ruptura
                                  from apr in db.Apresentacao.Where(ap => ap.Codigo_Apresentacao == rup.Codigo_Apresentacao)
                                  from pdv in db.PDV.Where(p => p.CodigoPDV == rup.CodigoPDV)
                                  from mot in db.Motivo.Where(m => m.IDMotivo == rup.IDMotivo)

                                  select new {
                                      rup.IDRuptura,
                                      rup.DataRuptura,
                                      rup.IDMotivo,
                                      mot.Motivo1,
                                      rup.IDOrigem,
                                      rup.CodigoPDV,
                                      pdv.UF,
                                      pdv.Cidade,
                                      pdv.Cnpj + " - " + pdv.Descricao as PDV,==>> Erro aqui
                                      rup.Codigo_Apresentacao,
                                      apr.Unidade_Negocio,
                                      apr.Franquia,
                                      apr.Descricao}).ToList().Distinct();
  • I think this is it: store = pdv. Cnpj + " - " + pdv.Description

1 answer

5


In C#, an anonymous object can be written as follows:

var a = new { texto, numero };

and . net will be in charge of creating an object with properties with the names of the references you passed as properties, texto and numero. Or you can name the properties, for example:

var a = new { MeuText = texto, MeuNumero = numero };

And will be accessible by the properties MeuTexto and MeuNumero.

You can also merge.

var a = new { MeuText = texto, numero };

In your case, add a new property, because . Net will not know how to rename the property, since it is dynamic (concatenates two fields).

var monta_arvore = (from rup in db.Ruptura
                    from apr in db.Apresentacao.Where(ap => ap.Codigo_Apresentacao == rup.Codigo_Apresentacao)
                    from pdv in db.PDV.Where(p => p.CodigoPDV == rup.CodigoPDV)
                    from mot in db.Motivo.Where(m => m.IDMotivo == rup.IDMotivo)    
                    select new {
                                rup.IDRuptura,
                                rup.DataRuptura,
                                rup.IDMotivo,
                                mot.Motivo1,
                                rup.IDOrigem,
                                rup.CodigoPDV,
                                pdv.UF,
                                pdv.Cidade,
                                CnpjDescricao = pdv.Cnpj + " - " + pdv.Descricao,
                                rup.Codigo_Apresentacao,
                                apr.Unidade_Negocio,
                                apr.Franquia,
                                apr.Descricao})
                  .ToList()
                  .Distinct();
  • According to my comment, I had already answered. I missed a little more research on my part. I failed to ask before I break my head due I have to solve in minutes, but I did and I apologize for the quick post without any research.

  • Tranquil @pnet, sometimes this doubt may be the same as other users and here is the record!. If possible mark as answer to indicate the path :) Thank you!

Browser other questions tagged

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