Eliminar elementos de un diccionario según la selección del cuadro de lista

I have a listbox that prints the name of a custom item class

public class Item
    public string @Url { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }

    public Item(string @url, string name, double price)
        this.Url = url;
        this.Name = name;
        this.Price = price;

    public override string ToString()
        return this.Name;

I tried the normal method but because i have radio buttons to sort the list box it messes it up since index is changed.


//new item is declared
Dictionary<int, Item> itemList = Dictionary<int, Item> { new Item("f.ca", "name1", 33);
                                                      new Item("m.ca", "name2", 44); }
//Items added to listbox
for (int v = 0; v < itemList.Count; v++)

//start sorting
var priceSort = from item in itemList
                orderby item.Value.Price
                select new { item.Value.Name, item.Value.Price };

foreach (var i in priceSort)
//end sorting listbox updated

now that the new list is created removing only the item in itemlist is necessary since the box is updated.

/* This code is what i thought but SelectedIndex say if on 0 and since the sorted by price */

The issue being now its trying to remove items[0] when items[1] is really the one that needs to be removed. Is there a way i could make it compare the string of the itemlistbox to the .Name property of the items dictionary?

preguntado el 09 de septiembre de 13 a las 23:09

1 Respuestas

You stated that the key for your dictionary is determined by the current count of items in the dictionary. If that's the case, you'd have to do something like this:

var matches = itemList.Where(x => x.Name == itemListBox.SelectedValue);
if (matches.Any())

But this is slow and inelegant. You're really not using the Dictionary class correctly. Dictionaries are ideal for performing quick access based on a known key value. If you have to search for the key every time, you loose all benefit the Dictionary provides.

You might as well use a simple List<Item> en su lugar, usando el FindIndex / RemoveAt métodos:

var index = itemList.FindIndex(x => x.Name == itemListBox.SelectedValue);
if (index != -1)

This isn't a whole lot faster, but it's more elegant—lists are specifically designed to support this kind of thing without having to resort to Linq.

Or better yet, use the item's name as the dictionary key:

Dictionary<string, Item> itemList = Dictionary<string, Item>();
itemList.Add("name1", new Item("f.ca", "name1", 33));
itemList.Add("name2", new Item("m.ca", "name2", 44));



This is a much more efficient and elegant solution.

Respondido el 10 de Septiembre de 13 a las 15:09

I will say it again. Thank you for the input but if this is a dictionary things are not in an indexable list as its organized a different way. Sorry - MikaAK

Sorry, your question was a bit confusing at first. Thanks for updating it, however, it's still not clear how you're creating your dictionary. The code you posted won't compile, so I can't tell how the key is determined. Also, is itemList diferente de items? - pswg

No its not that was my mistake sorry. The key is determined by item.Count so it always creates it 0 - arbitrary number. - MikaAK

@user2434321 See my updated answer for alternative solutions. - pswg

Yes this is what i concluded to, I have changed the data type back to a list and have my program almost fully working. Now to just integrate saving and opening - MikaAK

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.