How do I update an item from a generic list?

5

I'm trying to change the name and email of a certain item from the list below, but found no way other than to remove the item from the list and add it upgraded again. Is there another way to update?

class Program
{
    static void Main(string[] args)
    {
        List<Aluno> aluno = new List<Aluno>{
            new Aluno() { AlunoId = 1, Nome = "Cláudia",Email="[email protected]" },
            new Aluno() { AlunoId = 2, Nome = "Pedro",Email="[email protected]" },
            new Aluno() { AlunoId = 3, Nome = "Eduardo",Email="[email protected]" }
        };

        Console.WriteLine("==================================");

        foreach (var item in aluno)
        {
            Console.WriteLine("ID: {0}\nNome: {1}\nEmail: {2}", item.AlunoId, item.Nome,item.Email);
            Console.WriteLine("==================================");
        }

        Console.Read();
    }
}

class Aluno
{
    public int AlunoId { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
}
    
asked by anonymous 16.09.2015 / 20:48

2 answers

7

There is not much secret. Do you know how to update array ? It's the same thing:

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

public class Program {
    public static void Main(string[] args) {
        List<Aluno> aluno = new List<Aluno>{
            new Aluno() { AlunoId = 1, Nome = "Cláudia",Email="[email protected]" },
            new Aluno() { AlunoId = 2, Nome = "Pedro",Email="[email protected]" },
            new Aluno() { AlunoId = 3, Nome = "Eduardo",Email="[email protected]" }
        };

        WriteLine("==================================");

        foreach (var item in aluno) {
            WriteLine("ID: {0}\nNome: {1}\nEmail: {2}", item.AlunoId, item.Nome,item.Email);
            WriteLine("==================================");
        }

        aluno[0].Nome = "João";
        aluno[0].Email = "[email protected]";

        foreach (var item in aluno) {
            WriteLine("ID: {0}\nNome: {1}\nEmail: {2}", item.AlunoId, item.Nome,item.Email);
            WriteLine("==================================");
        }
    }
}

class Aluno {
    public int AlunoId { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
}

See working on dotNetFiddle .

There you are changing the first item, but you can change whatever you want, it can be through a variable in the index or even make a search on some element. There are even methods ready for this, but that's another matter.

    
16.09.2015 / 20:58
5

To change the first student data in the list, you should do the following:

aluno[0].Nome = "NovoNome";
aluno[0].Email = "[email protected]";

To change other positions, you only need to change 0 to index for the position you want to change.

Note: Whenever you access a position of a array , List or IEnumerable , the count starts with index 0 . So if your Collection has 10 positions, the last index will be 9 .

Edit

In addition to being able to indexes to locate the item you want to change in the list, you can include ( using ) namespace System.Linq and make queries (much like sql) within from your list. For example, to find the student named John :

var joao = aluno.Single(x => x.Nome == "João");
//ou
var joao = aluno.First(x => x.Nome == "João");

Single() and First() do pretty much the same thing, they only return a record that meets the condition. However, when there is more than one record that meets the condition, Single() throws exception and First() returns the first one to find.

Edit 2

In addition to the methods mentioned above, there is FirstOrDefault() . It has behavior similar to First() , the only difference is that First() throws exception if no item meets condition, while FirstOrDefault() returns null . The usage is exactly the same:

var joao = aluno.FirstOrDefault(x => x.Nome == "João");
    
16.09.2015 / 20:53