Search code examples
c#listlinqcollections

C# How to remove object form list using keyword?


I have this code

 monthly[i] = new MonthlyEmployee();  
 monthly[i].ID = Console.ReadLine();
 monthly[i].Name = Console.ReadLine();
 monthly[i].Fee = int.Parse(Console.ReadLine());

 //Add to list
 listemployee.Add(mothly[i]);

I have another object which similar with this code and they are in the same list Employee. I want to add menu to remove the employee, but the user should input the ID. So, The user can choose which employee they want to remove. But, I don't know the way to access the employeee ID. Thanks, I really appreciate your help.


Solution

  • You can use this monthly.RemoveAll(x => x.ID == ID);.

    You can refer this link, lot of good solutions can be found.