How to update an item from a generic list?

Asked

Viewed 5,391 times

7

I am trying to change the name and email of a particular item in the list below, but I couldn’t find another way to remove the item from the list and add it again updated. There is 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; }
}

2 answers

9


Not much secret. Knows 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) {
        var 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; }
}

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

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

6

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

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

To change other positions, you just need to change the 0 for index the position you wish to change.

Note: Whenever accessing a position of a array,List or IEnumerable and the counting begins with index 0. Soon, if your Collection has 10 positions, the last index will be 9.


Besides being able to right to locate the item you want to change in the list, you can include (using) the namespace System.Linq and make queries (very similar to sql) within your list. As, for example, to search for the student with name John:

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

The Single() and the First() do practically the same thing, they return only one record that meets the condition. However, when there is more than one record that meets the condition o Single() spear a exception and the First() returns the first one you find. You can see about this in the post: In the Entity Framework, the Singleordefault() and Firstordefault() methods present different behaviors?


In addition to the above methods, there are FirstOrDefault() and SingleOrDefault(). They have similar behaviors First() and Single, with the only difference that they don’t burst exception if no item meets the condition, but return null.

The use is exactly the same:

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

Browser other questions tagged

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