-1

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.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Lin
  • 23
  • 3
  • 1
    Is it a `List<>`? Then maybe you can use `monthly.RemoveAll(e => e.ID == idToRemove);`. But maybe you should use a `Dictionary<,>` instead where the ID were the key? – Jeppe Stig Nielsen Jun 26 '20 at 13:45

2 Answers2

1

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

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

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
-1

You can use something like this to get the item first and then remove it

var itemToRemove = listemployee.SingleOrDefault(x => x.ID == ID);
if (itemToRemove != null)
    listemployee.Remove(itemToRemove);
Herberth Gomez
  • 187
  • 1
  • 2
  • 19