List of objects generating string

Asked

Viewed 39 times

1

I have an object list Person that contains a list of Endereços. I would like to print this person with the addresses on string.

       var pessoa = new Pessoa() { Nome = "Jão" };

        var e = new Endereco() { Cidade = "São Paulo", Estado = "SP", Pessoa = pessoa };
        var e1 = new Endereco() { Cidade = "Rio de Janeiro", Estado = "RJ", Pessoa = pessoa };
        var e2 = new Endereco() { Cidade = "Belo Horizonte", Estado = "MG", Pessoa = pessoa };

        pessoa.Enderecos.Add(e);
        pessoa.Enderecos.Add(e1);
        pessoa.Enderecos.Add(e2);

        string result = string.Empty;
        foreach (var item in pessoa.Enderecos)
        {
            result += item.Cidade + " | " + item.Estado + " | ";
        }

        System.Console.WriteLine($"Endereços: {result}");

My result is correct:

Addresses: São Paulo | SP | Rio de Janeiro | RJ | Belo Horizonte | MG |

I would like to print addresses in a single string using LINQ instead of foreach with the same output result I got, it would be possible?

    string result = string.Empty;
    foreach (var item in pessoa.Enderecos)
    {
        result += item.Cidade + " | " + item.Estado + " | ";
    }

1 answer

1


Your code improved and with the LINQ option:

using static System.Console;
using System.Collections.Generic;
using System.Text;

public class Program {
    public static void Main() {
        var pessoa = new Pessoa() { Nome = "Jão" };
        pessoa.Enderecos = new List<Endereco> {
            new Endereco() { Cidade = "São Paulo", Estado = "SP", Pessoa = pessoa },
            new Endereco() { Cidade = "Rio de Janeiro", Estado = "RJ", Pessoa = pessoa },
            new Endereco() { Cidade = "Belo Horizonte", Estado = "MG", Pessoa = pessoa }
        };
        var result = new StringBuilder(pessoa.Enderecos.Count * 36); //tamanho máximo de cada cidade
        foreach (var item in pessoa.Enderecos) result.Append(item.Cidade + " | " + item.Estado + " | ");
        WriteLine($"Endereços: {result}");
        result.Clear();
        pessoa.Enderecos.ForEach(item => result.Append(item.Cidade + " | " + item.Estado + " | "));
        WriteLine($"Endereços: {result}");
    }
}

public class Pessoa {
    public string Nome;
    public List<Endereco> Enderecos;
}

public class Endereco {
    public string Cidade;
    public string Estado;
    public Pessoa Pessoa;
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Your code is falling from the problem of Shlemiel the Painter’s Algorithm and makes the code rather inefficient. In some cases it may not be as critical, but I wouldn’t risk without.

I haven’t packed the guy’s suit yet Pessoa let create object in invalid state, among other problems that are not worth putting here, because some depends a little on context as expose implementation details, or cross-reference between the person and the address (there is a case for use, but it is almost always a mistake, but if need ensures that is leaking abstraction).

And careful, liking and being right are two different things, LINQ is called Language Integrated Query. That is, it is made for queries and not for processing. This case makes sense, but many people start using the ForEach() for processing and is wrong, even if it works.

Browser other questions tagged

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