How to concatenate properties of a single List<> with LINQ?

Asked

Viewed 529 times

2

I need to concatenate two properties of each item on my list, in the example below it works but I would like to know how I can do the same thing using LINQ instead of foreach?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        List<Pessoas> lstPessoas = new List<Pessoas>();
        Pessoas pessoa = null;

        pessoa = new Pessoas();
        pessoa.Id = 1;
        pessoa.Nome = "Mauricio";
        lstPessoas.Add(pessoa);

        pessoa = new Pessoas();
        pessoa.Id = 2;
        pessoa.Nome = "João";
        lstPessoas.Add(pessoa);

        pessoa = new Pessoas();
        pessoa.Id = 3;
        pessoa.Nome = "Maria";
        lstPessoas.Add(pessoa);

        foreach (Pessoas item in lstPessoas)
        {
            item.Nome = item.Id + " - " + item.Nome;
        }
    }
}

public class Pessoas
{
    public int Id { get; set; }
    public string Nome { get; set; }
}
  • I thought it was Linq-to-sql I’m still new to the subject. Thank you

1 answer

3


Do you see any reason to do this? This generates side effect, that is, some data is modified, so LINQ is not suitable. Q there is of query, ie, query. When you want to do more than one query the best is to use the command for each same. LINQ should not be used when it does not bring clear benefits. If you will insist you can use the method ForEach():

lstPessoas.ForEach(item => item.Nome = item.Id + " - " + item.Nome);

Code:

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

public class Program {
    public static void Main() {
        var lstPessoas = new List<Pessoas>();

        var pessoa = new Pessoas {
            Id = 1,
            Nome = "Mauricio" };
        lstPessoas.Add(pessoa);

        pessoa = new Pessoas {
            Id = 2,
            Nome = "João" };
        lstPessoas.Add(pessoa);

        pessoa = new Pessoas {
            Id = 3,
            Nome = "Maria" };
        lstPessoas.Add(pessoa);

        lstPessoas.ForEach(item => item.Nome = item.Id + " - " + item.Nome);
        foreach (var item in lstPessoas) {
            WriteLine(item.Nome);
        }
    }
}

public class Pessoas {
    public int Id { get; set; }
    public string Nome { get; set; }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference. Interestingly I made one for each to display the result and LINQ there would even make more sense.

  • Dude, this is exactly what I wanted, this snippet of code you pasted is right? In my view this way that you presented in terms of performance seems to be faster and leaves the code cleaner. Thank you very much hug.

  • Yes, it’s LINQ. This is slower than using the command. There is controversy if the code gets cleaner. It gets slightly smaller. But it is easy to fall into traps when there is side effect using this way. Of course in a simple example you will not have problems. Some cases don’t even work.

  • Thank you I didn’t know:).

Browser other questions tagged

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