且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从列表集合中删除对象?

更新时间:2023-12-04 20:27:16

List<T>将用于比较对象引用(除非SelectListItem实现自定义的相等方法).因此,除非您仍然对第二个项目有参考,否则您将不得不通过参考或查找所需项目

List<T> is, by default, going to be comparing object references (unless SelectListItem implements a custom equality method). So unless you still have a reference to the second item around, you are going to have to get it either by reference, or by finding the desired item:

var item = myList.First(x=>x.Value == "two");
myList.Remove(item);

索引可能会更容易...

Index may be easier...