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

C# 根据列表框选择从字典中删除项,c#,.net,dictionary,listbox,C#,.net,Dictionary,Listbox,我有一个列表框,用于打印自定义项类的名称 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; th

我有一个列表框,用于打印自定义项类的名称

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;
    }
}
/* This code is what i thought but SelectedIndex say if on 0 and since the sorted by price */
itemList.Remove(itemListBox.SelectedIndex);
我尝试了正常的方法,但是因为我有单选按钮来对列表框进行排序,因为索引被更改了,所以它会把它搞砸

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

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

现在的问题是,当项目[1]确实是需要删除的项目时,尝试删除项目[0]。是否有一种方法可以让它将itemlistbox的字符串与items字典的.Name属性进行比较

您声明字典的键由字典中当前项目的计数决定。如果是这样的话,你必须这样做:

/* This code is what i thought but SelectedIndex say if on 0 and since the sorted by price */
itemList.Remove(itemListBox.SelectedIndex);
var matches = itemList.Where(x => x.Name == itemListBox.SelectedValue);
if (matches.Any())
{
    itemList.Remove(matches.First().Key);
}
但这是缓慢和不雅的。您确实没有正确使用Dictionary类。字典是基于已知键值执行快速访问的理想工具。如果您每次都要搜索密钥,那么您就失去了字典提供的所有好处

/* This code is what i thought but SelectedIndex say if on 0 and since the sorted by price */
itemList.Remove(itemListBox.SelectedIndex);
您不妨使用一个简单的
列表
,使用/方法:

/* This code is what i thought but SelectedIndex say if on 0 and since the sorted by price */
itemList.Remove(itemListBox.SelectedIndex);
这并不是说要快很多,但更优雅的列表是专门为支持这类事情而设计的,而不必求助于Linq

/* This code is what i thought but SelectedIndex say if on 0 and since the sorted by price */
itemList.Remove(itemListBox.SelectedIndex);
或者更好地使用项的名称作为字典键:

/* This code is what i thought but SelectedIndex say if on 0 and since the sorted by price */
itemList.Remove(itemListBox.SelectedIndex);
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);
Dictionary itemList=Dictionary();
项目列表。添加(“名称1”,新项目(“f.ca”,“名称1”,33));
添加(“名称2”,新项目(“m.ca”,“名称2”,44));
...
itemList.Remove(itemListBox.SelectedValue);

这是一个更高效、更优雅的解决方案。

我再说一遍。感谢您的输入,但如果这是一本词典,则不在可索引列表中,因为它的组织方式不同。对不起,你的问题一开始有点让人困惑。谢谢你的更新,不过,你是如何创建字典的还不清楚。您发布的代码无法编译,因此我无法说明密钥是如何确定的。另外,
itemList
items
不同吗?不,不是我的错,对不起。键由item.Count决定,因此它总是创建0-任意数。@user2434321请参阅我的更新答案以获得替代解决方案。是的,这就是我的结论,我已将数据类型更改回列表,并且使我的程序几乎完全工作。现在只需将保存和打开集成起来
/* This code is what i thought but SelectedIndex say if on 0 and since the sorted by price */
itemList.Remove(itemListBox.SelectedIndex);