Avoid duplicated id in list c#

Asked

Viewed 304 times

0

I am doing a list exercise where I have to enter the employee id, but there should be no repetition of id. I would like to know how to use the foreach to avoid checking and blocking if a repeated value is inserted.

Follows the code of the executable class:

Console.WriteLine("Quantos empregados serão registrados?");
int n = int.Parse(Console.ReadLine());

        List<Empregado> lista = new List<Empregado>();

        for(int i=1; i<=n; i++)
        {
            Console.WriteLine("Empregado #{0}",i);

            Console.WriteLine("Digite o ID:");
            int id = int.Parse(Console.ReadLine());
            foreach (int obj in lista)
            {
                if (id!=null)
                {
                    Console.WriteLine("Id já existe, digite outro:");
                    id = int.Parse(Console.ReadLine());

                }
        }
  • And can’t it be done any other way? Has some restriction of what you can use?

  • I asked the teacher and he answered this:"What you can do is scroll through the list to see if there is already the id being informed. For this, you can make a foreach loop in the list to perform this validation, beauty?" But I didn’t understand how to do this.

  • But this is a bad way to do, there is some restriction?

  • if there is an easier way can be tbm, just wanted to know how to avoid the same duplicity

1 answer

1

I’ll show you a way you can achieve what you want to do.

That way you can use the Linq to check if the entered id already exists in your list:

//Usando linq para verivicar se o id digitado já existe na sua lista
if (lista.Any(x=> x.Id == id))
{
    Console.WriteLine("Id já existe, digite outro:");
    i--; //Se existe, decrementa o contador para que ele possar informar novamente
}
else
{
    //Adicione aqui seu novo Empregado a sua Lista
}

In case you haven’t learned about Linq and you don’t understand what you’re doing, try to continue your implementation using the for or foreach. It’s important that you understand what you’re doing.

Note that in your code in the question there are problems and neither the implementation with the foreach you achieved.

Here for example: foreach (int obj in lista) you are trying to go through your list of type Empregado, but it’s setting her up as int. That won’t work.

Another, less serious problem is this: if (id!=null) where your id is of type int and will never be null. Read here if you want to understand more about it too.

  • 2

    I don’t think it’s the case with the grid, but you’re selecting all the ids from the list, converting to an array, then doing a Where where will return all the items from the list if the array contains the informed id, then converting it to another list, and checks if the quantity is greater than 0... complicated into... enough: if (lista.Any(x=> x.Id == id)) { }

  • @Rovann Linhalis I was going to recommend exactly that, but when I come to the comment I see that they have already done it.

  • 1

    @Rovannlinhalis, I edited with the form you suggested if you don’t mind. Much simpler even.

  • 1

    The list is unnecessary there George.

Browser other questions tagged

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