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

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

// this is in a static method. List mySTring = new List(); mySTring.add("one"); mySTring.add("two"); 

但是在我的一个页面上,我有一个不需要字段“2”的下拉列表,所以我所做的只是编写重复的代码而不是

 myString.remove("two"); 

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

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

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

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

谢谢

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

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

索引可能更容易……

您可以使用RemovalAll方法:

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

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

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

进入jquery自动完成function

在这里我使用了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 }});