简体   繁体   English

如何从 c# 的列表中删除特定项目?

[英]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.在这种方法中,我比较了两个列表数据,这就是为什么使用 object p 和 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.使用这种方法,我可以添加东西.. 如果我想从 projectemployee 列表中删除添加的数据,谁能告诉我该怎么做。

     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我已经尝试过了,但它没有删除特定项目

It looks like you've copy pasted the code from Add, which looks like看起来你已经从 Add 复制粘贴了代码,看起来像

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 inside if实际上是 "and" 所以这个检查是

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:但是,我建议您更改逻辑。尝试根据 x 或 y 从列表中获取员工,然后根据您是否获得default (null) 做出决定:

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. p现在肯定是匹配条件的实际列表项,或者如果没有匹配项,则它是 null。 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"原因是“仅仅因为您创建了两个内部具有相同数据的 class 实例,并不意味着当您要求列表删除一个时,列表将它们视为相同”

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.除非您覆盖了 C# 对象使用的默认 Equals,否则如果两个对象所在的 memory 地址相等,则两个对象将被视为相等。 In the above case of two new objects they are not equal because they will be at different memory addresses在上述两个新对象的情况下,它们不相等,因为它们将位于不同的 memory 地址

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: Remove使用 Equals 测试列表中的每个项目是否等于传入的项目。如果您没有提供自己的 Equals 方法来比较 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 ..然后您可以改为从列表中检索确切的项目,并将其传递回删除,以便要求删除的 object 的 memory 地址相同(因此列表最终执行“a 等于 b”方案而不是“x 等于 y”的场景

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如果您确定您已经处于“a 等于 b”的场景中,您可以执行ProjectEmployeeList.Remove(projectEmployee)但如果它是“键入要删除的员工的 id,请从中创建一个新员工,询问要删除的列表”,您将根据 Id 从列表中检索员工

Footnote.脚注。 If you override Equals, always override GetHashCode too如果你覆盖 Equals,也总是覆盖 GetHashCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM