c# - Combine lists of key value pairs where keys match -


i have list of list<keyvaluepair<datetime, int>>, want merge 1 (union), , there pairs same key in more 1 list, have 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 i've been reading linq trickery sort of thing possible neatly, far i've not been able head around it. if there's nice way linq i'd love hear it, if not i'll grateful other tidy looking answer - c++ brain can think of long-winded procedural ways of doing :)

note i'm using list reason please don't suggest using dictionary or datatype. thanks.

ilookup<datetime, int> lookup = source   .selectmany(list => list)   .tolookup(kvp => kvp.key, kvp => kvp.value); 

or

list<keyvaluepair<datetime, int>> result = source   .selectmany(list => list)   .groupby(kvp => kvp.key, kvp => kvp.value)   .select(g => new keyvaluepair<datetime, int>(g.key, g.sum()))   .tolist(); 

Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -