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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1698130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:03:05  来源:igfitidea点击:

How to remove an object from a list collection?

c#.netasp.netasp.net-mvc

提问by chobo2

I had to change on my lines of code around. I before had something like this

我不得不改变我的代码行。我以前有过这样的事情

// this is in a static method.
List<string> mySTring = new List<string>();

mySTring.add("one");
mySTring.add("two");

However on one of my pages I have a dropdownlist that does not require the field "two" so instead of writing duplicate code all I did was

但是,在我的其中一个页面上,我有一个不需要字段“二”的下拉列表,因此我所做的不是编写重复的代码

myString.remove("two");

Now I need to change my list to a List<SelectListItem> myList = new List<SelectListItem>();

现在我需要将我的列表更改为 List<SelectListItem> myList = new List<SelectListItem>();

So I have it now looking like this:

所以我现在看起来像这样:

  List<SelectListItem> myList = new List<SelectListItem>()
            { 
                new SelectListItem() { Text = "one", Value = "one"},
                new SelectListItem() { Text = "two", Value = "two"},
            };

So now how do I remove the selectListItem that contains "two"? I know I probably could use remove by index. But I might add to list in the future so I don't want to start hunting down and changing it if the index changes.

那么现在如何删除包含“两个”的 selectListItem ?我知道我可能可以使用按索引删除。但是我将来可能会添加到列表中,因此如果索引发生变化,我不想开始寻找和更改它。

Thanks

谢谢

采纳答案by Marc Gravell

List<T>is, by default, going to be comparing object references (unless SelectListItemimplements 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 findingthe desired item:

List<T>默认情况下,将比较对象引用(除非SelectListItem实现自定义相等方法)。因此,除非您仍然有对第二项的引用,否则您将不得不通过引用或找到所需的项来获取它:

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

Index may be easier...

索引可能更容易...

回答by Matt Hamilton

You could use the RemovalAllmethod:

您可以使用RemovalAll方法:

myList.RemoveAll(i => i.Text == "two");

Obviously this will get rid of allthe items whose "Text" property is "two", but since you're using it in a ComboBox I'm assuming you'll only have one item for each "Text" value.

显然,这将删除“Text”属性为“two”的所有项目,但由于您在 ComboBox 中使用它,我假设每个“Text”值只有一个项目。

回答by Donil

we can make mouse selection over the autocomplete div by adding this following code

我们可以通过添加以下代码在自动完成 div 上进行鼠标选择

into the jquery autocomplete function

进入jquery自动完成功能

here i have used the id name as Requiredid

在这里,我使用了 ID 名称作为Requiredid

 $("#Requiredid").autocomplete({focus:function(e,ui) {
      document.getElementById('Requiredid').value = ui.item.label;
      document.getElementById('hdnValue').value = ui.item.id;//If You need this value you //can add this line
}});