简体   繁体   English

根据列表框选择从字典中删除项目

[英]Remove items from a dictionary based on listbox selection

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. 我尝试了普通方法,但是因为我有单选按钮来对列表框进行排序,所以自从索引更改以来,它就使它混乱了。

EG 例如

//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++)
{
    itemListBox.Items.Add(itemList[v].Name);
}

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

itemListBox.Items.Clear();
foreach (var i in priceSort)
{
    itemListBox.Items.Add(i.Name);
}              
//end sorting listbox updated

now that the new list is created removing only the item in itemlist is necessary since the box is updated. 现在,由于创建了新列表,因此创建了新列表,因此仅需要删除itemlist中的项目。

/* This code is what i thought but SelectedIndex say if on 0 and since the sorted by price */
itemList.Remove(itemListBox.SelectedIndex);

The issue being now its trying to remove items[0] when items[1] is really the one that needs to be removed. 现在的问题是,当items [1]确实是需要删除的项目时,它试图删除items [0]。 Is there a way i could make it compare the string of the itemlistbox to the .Name property of the items dictionary? 有没有办法让我将itemlistbox的字符串与Items词典的.Name属性进行比较?

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())
{
    itemList.Remove(matches.First().Key);
}

But this is slow and inelegant. 但这是缓慢而优雅的。 You're really not using the Dictionary class correctly. 您确实没有正确使用Dictionary类。 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> instead, using the FindIndex / RemoveAt methods: 您也可以使用简单的List<Item>来代替,而使用FindIndex / RemoveAt方法:

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

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. 这并没有快很多,但是更优雅-列表是专门为支持这种事情而设计的,而不必诉诸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));

...

itemList.Remove(itemListBox.SelectedValue);

This is a much more efficient and elegant solution. 这是一个更有效,更优雅的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM