I have a couple of dictionaries that I want to perform intersection on. two dictionaries might have different lists of values for the 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 I want the resultant dictionary to contain only:
[1],[list<6,7>]
How can I achieve this? The actual data structure where I want the intersection is of the form:
Dictionary<long, SortedList<int,List<long>>>
You can achieve this as follows:
var result = Primary.Keys.Intersect(Secondary.Keys)
.ToDictionary(key => key,
key => Primary[key].Intersect(Secondary[key]).ToList());
Transforming the result into Dictionary<long, SortedList<int,List<long>>>
should be easy enough.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments