How to not save value in the list if(seal)

Asked

Viewed 25 times

0

i have a foreach running through my list and I want if() to be true it shows a message saying that there is already this value in the list and DON’T STORE this value on the list, someone knows how to do ??

foreach (Agendamento agenda in Agendamentos)
            {
                if (agenda.matricula.Equals(matricula) && agenda.horarioAtendimento.ToString("dd/MM/yyyy").Equals(maisDeUmDia))
                {
                    Console.WriteLine("JA EXISTE UM ATENDIMENTO PARA ESTA DATA");
                }
                else
                {
                    Console.WriteLine("NÃO EXISTE UM ATENDIMENTO PARA ESTA DATA");
                }
            }
  • I recommend using ready-made methods like .Any or .Contains

1 answer

0

After instantiating the Agedamento agenda; and assign its values, you can use the method .Contains() from the list - to check if Schedule already exists.

An example:

if (Agendamentos.Contains(agenda))
{
    Console.WriteLine("JÁ EXISTE UM ATENDIMENTO PARA ESTA DATA");
}
else
{
    Agendamentos.Add(agenda);
    Console.WriteLine("ATENDIMENTO REGISTRADO");
}

Browser other questions tagged

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