从列表集中删除项目而不删除

Mur*_*san 2 c# asp.net collections list

我正在研究一个系列.我需要从集合中删除一个项目并使用过滤/删除的集合.

这是我的代码

public class Emp{
  public int Id{get;set;}
  public string Name{get;set;}
}

List<Emp> empList=new List<Emp>();
Emp emp1=new Emp{Id=1, Name="Murali";}
Emp emp2=new Emp{Id=2, Name="Jon";}
empList.Add(emp1);
empList.Add(emp2);

//Now i want to remove emp2 from collection and bind it to grid.
var item=empList.Find(l => l.Id== 2);
empList.Remove(item);
Run Code Online (Sandbox Code Playgroud)

问题是甚至在删除项目后我的收藏仍显示计数2.
可能是什么问题?

编辑:

原始代码

  var Subset = otherEmpList.FindAll(r => r.Name=="Murali");

   if (Subset != null && Subset.Count > 0)
   {
    foreach (Empl remidateItem in Subset )
    {
       Emp removeItem = orginalEmpList.Find(l => l.Id== 
                                          remidateItem.Id);
                    if (removeItem != null)
                    {
                        orginalEmpList.Remove(remidateItem); // issue here

                    }
      }
    }
Run Code Online (Sandbox Code Playgroud)

它工作正常.在实际代码中我删除了remediateItem.remediateItem是同一类型,但它属于不同的集合.

Adi*_*dil 7

您传递的对象Remove不在您尝试删除的列表中,而是将对象复制到其他列表中,这就是为什么它们不被删除,使用List.RemoveAll方法传递谓词.

lst.RemoveAll(l => l.Id== 2);
Run Code Online (Sandbox Code Playgroud)

如果你想删除一些其他集合中的许多id,比如id数组

int []ids = new int[3] {1,3,7};
lst.RemoveAll(l => ids.Contains(l.Id))
Run Code Online (Sandbox Code Playgroud)