Label in loop in Foreach

3

I'm having difficulty displaying information that comes from an object to show on a Label, returning it as a list.

//Pegando os dados do Rest e armazenando na variável usuários

var usuario = response.Content.ReadAsAsync<IEnumerable<ConsumeRoot>>().Result;

foreach (var a in usuario)
{
    lblTitulo.Text = a.titulo;
}

The label returns only one information, this class is receiving an API that is receiving information from a database, it is 6 information that is to appear on that stack inform.

    
asked by anonymous 27.07.2018 / 14:59

2 answers

5

At each iteration of this foreach, you are replacing the previous information, perhaps you are forgetting to concatenate the results:

    foreach (var a in usuario)
    {
        lblTitulo.Text += a.titulo + " ";
    }
    
27.07.2018 / 15:03
7

It turns out that the value of the label is being replaced with each loop.

Concatenate the values or use string.Join and make only one assignment

var usuario = response.Content.ReadAsAsync<IEnumerable<ConsumeRoot>>().Result;

lblTitulo.Text = string.Join(", ", usuario.Select(u => u.Titulo));
    
27.07.2018 / 15:03