coder
coder

Reputation: 1

How to remove particular item from list in c#?

 public List<ProjectEmployee> ProjectEmployeeList { get; set; } = new List<ProjectEmployee>(); 
 //List for Add employee to project

        public string EProjectId { get; set; }//projectemployee variables
        public string EmployeePid { get; set; } //projectemployee variables 

        Project1 p = new Project1(); //object for projectlist
        Employee1 e = new Employee1();//object for employee list

  public void AddProjectEmployee(ProjectEmployee projectemployee) // to add employee to 
  project
    {

        if (!p.ProjectList.Any(p => p.ProjectId == projectemployee.EProjectId)) // condition 
                                 to check project id is already present or not in projectlist
          {
            if (!e.EmployeeList.Any(p => p.EmployeeId == projectemployee.EmployeePid)) // 
                     condition to check employee id is already present or not in employeelist
            {
                ProjectEmployeeList.Add(projectemployee); //both condition true then it will 
                         add the values

            }

In this method I am comparing two list data that is why used object p and object e.

            else
             {
                 Console.WriteLine("Not there"); //error message
             }
         }
     }

with this method I am able to add the things.. can anyone tell me what to do if I want to delete the added data from projectemployee list.

     public void DeleteEmployeeProject(ProjectEmployee projectemployee)
      {
        if (!ProjectEmployeeList.Any(p => p.EProjectId == projectemployee.EProjectId))                              
            {
            if (!ProjectEmployeeList.Any(p => p.EmployeePid == projectemployee.EmployeePid))                      
                 {
                ProjectEmployeeList.Remove(projectemployee); //(projectemployee); 
            }
            else
            {
                Console.WriteLine("Not there"); //error message
            }
        }
        foreach (var pe in ProjectEmployeeList)
        {
              Console.WriteLine($" Project id=|{pe.EProjectId}| +
                               Employee id= |{pe.EmployeePid}|");
                                            
         }
      }

I have tried this but its not removing particular item

Upvotes: 0

Views: 226

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

It looks like you've copy pasted the code from Add, which looks like

if not any employee has eproject id of blah
  If not any employee has employee pid of blah
    add employee 

if inside if is effectively "and" so this check is

if no employee has eproject id blah and no employee has pid blah
  add them

That looks great for an add operation - "if the employee isn't there, add them"

Doesn't look so good for a delete operation though - "if the employee isn't there, delete them"


I'd recommend you make a change to your logic, however.. Try and get the employee from the list based on x or y, and then make your decision based on whether you got default (null) or not:

var p == ProjectEmployeeList.FirstOrDefault(p => 
  p.EmployeePid == projectemployee.EmployeePid ||
  p.ProjectId == projectemployee.EProjectId
);

p is now certainly either the actual list item that matches the condition, or it is null if there was no matching item. You can then do:

//for add, the employee is not found/default if not there
if(p == default)
  ProjectEmployeeList.Add(projectemployee);

//for delete the employee is found/not-default if they are there
if(p != default)
  ProjectEmployeeList.Remove(p);

The reason is that "just because you make two instances of a class with the same data inside doesn't mean that a list will see them as the same when you're asking for it to remove one"

var x = new ProjectEmployee { EmployeePid = 1};

var y = new ProjectEmployee { EmployeePid = 1};

Console.Print(x.Equals(y)); //false 

Unless you've overridden the default Equals that C# objects use, two objects will be deemed equal if their memory addresses they live at are equal. In the above case of two new objects they are not equal because they will be at different memory addresses

var a = new ProjectEmployee { EmployeePid = 1};
var b = a;
Console.Print(a.Equals(b)); //true 

Remove uses Equals to test every item in the list for being equal to the one passed in. If you haven't provided your own Equals method that compares the ID:

override Equals(object other){
  return other is ProjectEmployee x && this.Id == x.Id; 
}

..then you can instead retrieve the exact item out of the list, and pass it back into Remove, so that the memory address of the object being asked to remove is the same (so the list ends up doing "a equals b" scenario rather than "x equals y" scenario

If you know for sure that you're already in an "a equals b" scenario, you can do ProjectEmployeeList.Remove(projectEmployee) but if it's a "type the id of the employee to remove, make a new employee from it, ask list to remove" you would retrieve the employee from the list based on Id

Footnote. If you override Equals, always override GetHashCode too

Upvotes: 1

Related Questions