How not to save m value in the if case list (true)

0

I have a foreach going through my list and I want it to be true if it shows a message saying that this value already exists in the list and DO NOT STORE this value in 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");
                }
            }
    
asked by anonymous 08.04.2018 / 19:28

1 answer

0

After instantiating Agedamento agenda; and assigning its values, you can use the .Contains() method of the list - to check if the Scheduling 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");
}
    
08.04.2018 / 23:24