Сте*_*чев 0 c# list duplicate-removal keyvaluepair
我在C#中有一个KeyValuePair列表格式化为string,int示例内容:
mylist[0]=="str1",5
mylist[2]=="str1",8
Run Code Online (Sandbox Code Playgroud)
我想要一些代码删除其中一个项目,另一个添加重复值.
所以它会是:
mylist[0]=="str1",13
Run Code Online (Sandbox Code Playgroud)
定义代码:
List<KeyValuePair<string, int>> mylist = new List<KeyValuePair<string, int>>();
Run Code Online (Sandbox Code Playgroud)
托马斯,我会尝试用伪代码解释它.基本上,我想要
mylist[x]==samestring,someint
mylist[n]==samestring,otherint
Run Code Online (Sandbox Code Playgroud)
变得:
mylist[m]==samestring,someint+otherint
Run Code Online (Sandbox Code Playgroud)
var newList = myList.GroupBy(x => x.Key)
.Select(g => new KeyValuePair<string, int>(g.Key, g.Sum(x=>x.Value)))
.ToList();
Run Code Online (Sandbox Code Playgroud)