c# - Intersection of Dictionary based on keys and values -
i have couple of dictionaries want perform intersection on. 2 dictionaries might have different lists of values same key.
example:
dictionary<int, list<int>> primary = new dictionary<int, list<int>>(); primary.add(1, new list<int>()); primary[1].add(5); primary[1].add(6); primary[1].add(7); primary.add(2, new list<int>()); primary[2].add(5); dictionary<int, list<int>> secondary = new dictionary<int, list<int>>(); secondary.add(1, new list<int>()); secondary[1].add(6); secondary[1].add(7); secondary[1].add(8); secondary.add(3, new list<int>()); secondary[3].add(5);
so want resultant dictionary contain only:
[1],[list<6,7>]
how can achieve this? actual data structure want intersection of form:
dictionary<long, sortedlist<int,list<long>>>
you can achieve follows:
var result = primary.keys.intersect(secondary.keys) .todictionary(key => key, key => primary[key].intersect(secondary[key]).tolist());
transforming result dictionary<long, sortedlist<int,list<long>>>
should easy enough.
Comments
Post a Comment