c# - How do I check if items already exist in a ToolStripMenuItem DropDownItems? -


items = file     .readlines(recentfiles)     .select(line => new toolstripmenuitem()     {         text = line     })     .toarray(); recentfilestoolstripmenuitem.dropdownitems.addrange(items); 

i want check if items exist in recentfilestoolstripmenuitem.dropdownitems

if not exist add if exist don't add.

you have 2 collections: items & recentfilestoolstripmenuitem.dropdownitems

using linq, should able an except() where() add difference between 2 collections.

this not tested.

recentfilestoolstripmenuitem.dropdownitems.addrange(items.except(recentfilestoolstripmenuitem.dropdownitems));

this tested

recentfilestoolstrip.dropdownitems.addrange(     items     .where(i => !recentfilestoolstrip.dropdownitems                  .oftype<toolstripmenuitem>()                  .select(t => t.text).contains(i.text)           ).toarray() ); 

slaks comment refers doing following:

recentfilestoolstripmenuitems.dropdownitems.clear(); recentfilestoolstripmenuitems.dropdownitems.addrange(items); 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -