基于C#中的相同列进行合并 [英] Merge based on same columns in C#
本文介绍了基于C#中的相同列进行合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的列表如
列表< account> = {{1,1},{1,2},{1,3},{2,3}}
所以现在我想输出像
list< acount> = {{1,6},{2,3} }
喜欢比较第一个元素如果相同则添加第二个元素
所以1在三个相同然后我添加所有1和得到6
所以请帮助我们获得这样的价值C#
我尝试了什么:
我试过循环,但它没有工作,因为我只有一个列表,为什么请帮助
I have list like
List<account>={{1,1},{1,2},{1,3},{2,3}}
So now i want output like
list<acount>={{1,6},{2,3}}
like compare first element if it is same then add second element
so 1 was same in three then i added all with 1 and got 6
so pls help to get value like this C#
What I have tried:
I have tried for loop but it is not working as i have only one list thats why please help
推荐答案
尝试:
Try:
List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>()
{ new KeyValuePair<int, int>(1, 1),
new KeyValuePair<int, int>(1, 2),
new KeyValuePair<int, int>(1, 3),
new KeyValuePair<int, int>(2, 3)};
list = list.GroupBy(kvp => kvp.Key).Select(g => new KeyValuePair<int, int>(g.Key, (int) g.Sum(x => x.Value))).ToList();
这篇关于基于C#中的相同列进行合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文