如何删除 <string, list<Items>> 字典的项目
How to remove Item for the dictonary of <string, list<Items>>
我有一本水果字典<string, list<Fruit>>
我的字典给出以下输出
Job ID: 1001 items:
"Apple"
"Mango"
"Banana"
"Pine Apple"
"Orange"
Job ID: 1002 items:
"Banana"
"Mango"
"Pine Apple"
"Orange"
"Apple"
Job ID: 1003 items:
"Apple"
"Banana"
"Orange"
"Pine Apple"
"Mango"
现在我想从水果列表中删除一个特定的水果
例如,我想从列表
中删除Apple
foreach(var i in fruitDic)
{
foreach(var k in i.Value)
{
// furitName is variable which I want to remove from list
if (k.Name == fruitName)
{
// here I am getting an error
k.Value.Remove(fruitName);
}
}
}
我收到一条错误消息:Error CS1503 Argument 1: cannot convert from 'string' to 'Practice.Models.Fruit'
你可以使用 RemoveAll
Removes all the elements that match the conditions defined by the
specified predicate.
给定
var dict = new Dictionary<string, List<Fruit>>();
用法
foreach (var list in dict.Values)
list.RemoveAll(x => x.Name == "SomeFunkyFruit");
我有一本水果字典<string, list<Fruit>>
我的字典给出以下输出
Job ID: 1001 items:
"Apple"
"Mango"
"Banana"
"Pine Apple"
"Orange"
Job ID: 1002 items:
"Banana"
"Mango"
"Pine Apple"
"Orange"
"Apple"
Job ID: 1003 items:
"Apple"
"Banana"
"Orange"
"Pine Apple"
"Mango"
现在我想从水果列表中删除一个特定的水果
例如,我想从列表
中删除Apple
foreach(var i in fruitDic)
{
foreach(var k in i.Value)
{
// furitName is variable which I want to remove from list
if (k.Name == fruitName)
{
// here I am getting an error
k.Value.Remove(fruitName);
}
}
}
我收到一条错误消息:Error CS1503 Argument 1: cannot convert from 'string' to 'Practice.Models.Fruit'
你可以使用 RemoveAll
Removes all the elements that match the conditions defined by the specified predicate.
给定
var dict = new Dictionary<string, List<Fruit>>();
用法
foreach (var list in dict.Values)
list.RemoveAll(x => x.Name == "SomeFunkyFruit");