How to remove an object from a collection of lists?

I had to change my lines of code. I used to have 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 drop-down list that does not require a “two” field, so instead of writing duplicate code, all I did was

myString.remove("two");

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

So now it looks like this for me:

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

So, how to remove the selectListItem element containing two? I know that maybe I can use delete by index. But I could add to the list in the future, so I don’t want to start searching and change it if the index changes.

thanks

+5
source share
3 answers

List<T> ( SelectListItem ). , , , :

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

...

+8

RemovalAll:

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

, , "" "", ComboBox, , "".

+6

div,

jquery

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
}});
0

All Articles