Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 14-Mar-16 22:14pm
Comments
Patrice T 15-Mar-16 3:36am    
Show code, define 'not working'

1 solution

Try:
C#
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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900