How to concatenate fields in linq and rename

1

I made a linq and it is not working the concatenation and the rename of 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();
    
asked by anonymous 09.09.2014 / 21:38

1 answer

4

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

var a = new { texto, numero };

and .NET will take care of creating an object with properties with the names of 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, as .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();
    
09.09.2014 / 21:53