Assign value to an object property by a list of objects

Asked

Viewed 2,070 times

1

I have a list of objects: List. Well, this object has three properties, say, cnpj, acao and novaacao. I did a lick or lick and brought me all the results, let’s say two lines. I do a foreach on the result of the Line or lambda and how now I assign to each property of my object through the list of that object? So:

List<pdv> lista = new List<pdv>();
var resultado = (from ...).ToList();

foreach(var r in resultado)
{
   lista.Add(r.cnpj);//ISSO DÁ ERRO
}

So I show only one cnpj, of which there are two. Always the last. new is the type PDV created before.

if (resultadoPdv.Count > 0 && resultadoAcao.Count > 0)
            {
                foreach (var r in resultadoPdv)
                {
                    novo.cnpj = r.Cnpj;
                    lista.Add(novo);
                }

                foreach (var r in resultadoAcao)
                {
                    novo.acao = r.acao;
                    novo.proximaAcao = r.proximaacao;
                    lista.Add(novo);
                }
            }

            return lista;

In this code, I can have two records, that is, the list has two lines, but with the same cnpj and I have two different cnpj.

foreach (var r in resultadoAcao)
                {
                    novo.acao = r.acao;
                    novo.proximaAcao = r.proximaacao;
                }

                foreach (var r in resultadoPdv)
                {
                    novo.cnpj = r.Cnpj;
                    lista.Add(novo);
                }

See how the web service return xml looks

<ArrayOfPdvNovo xmlns="http://schemas.datacontract.org/2004/07/V99SuporteTecnicoContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<PdvNovo>
<acao>Visita Técnica</acao>
<cnpj>03146478000112</cnpj>
<proximaAcao>Agendamento de Visita Técnica Parceiro</proximaAcao>
</PdvNovo>
<PdvNovo>
<acao>Visita Técnica</acao>
<cnpj>03146478000112</cnpj>
<proximaAcao>Agendamento de Visita Técnica Parceiro</proximaAcao>
</PdvNovo>
</ArrayOfPdvNovo>

By Harry’s reply, I changed it and it stayed that way:

PdvNovo novo = new PdvNovo();

            if (resultadoPdv.Count > 0 && resultadoAcao.Count > 0)
            {
                foreach (var r in resultadoAcao)
                {
                    novo.acao = r.acao;
                    novo.proximaAcao = r.proximaacao;
                    //lista.Add(novo);
                }

                foreach (var r in resultadoPdv)
                {
                    PdvNovo _cnpj = new PdvNovo();
                    _cnpj.cnpj = r.Cnpj;
                    _cnpj.acao = novo.acao;
                    _cnpj.proximaAcao = novo.proximaAcao;
                    lista.Add(_cnpj);
                }
            }
  • I’m asking this because if I return the type pdv and if I have two cnpj, it only shows one, the last, of course. And I need him to list all the cnpj in the question.

  • Gives error because the List is typed (pdv) and you’re passing text, I think you already know this?

  • novo ta instanciado fora do for?

  • Yes, it’s using the same instance! put inside of whatever it created for each item in the list a different instance, so there is repetition in your file. Look at my example I posted the solution!

1 answer

3


Thus:

List<pdv> lista = new List<pdv>();
var resultado = (from ...).ToList();

foreach(var r in resultado)
{
   pdv _item = new pdv(); // instancie dentro do for!
   _item.cnpj = r.cnpj; // da um enter aqui e vai colocando os outros campos ... 
   lista.Add(_item);
}

Browser other questions tagged

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