I have a list of List<KeyvaluePair<DateTime, int>>
, which I want to merge into one (union), and where there are pairs with the same key in more than one list, have their values summed.
Example:
input list 1:
date1, 1
date2, 2
input list 2:
date2, 3
date3, 4
input list 3:
date3, 5
date4, 6
desired output:
date1, 1
date2, 5
date3, 9
date4, 6
From what I've been reading about LINQ trickery this sort of thing is possible to do really neatly, but so far I've not been able to get my head around it. If there's a nice way to do this with LINQ I'd love to hear it, if not I'll be grateful for any other tidy looking answer too - my C++ brain can only think of long-winded procedural ways of doing this :)
Note that I'm using a List for a reason so please don't suggest using a dictionary or another datatype. Thanks.