I'm struggling with some linq to accomplish the following:
Dict1
1 0
2 0
3 1
4 1
5 2
6 2
7 2
Dict2
1 45
2 30
3 31
4 43
5 20
6 10
7 5
I want to sum the values in dictionary 2 based on the matching key in dictionary 1 and return the max or any default key with the results rolled up excluding values in dict 1 with a 0.
Eg:
New Dict
1 45
2 30
4 74
7 35
It sounds like you're effectively grouping by the value in dictionary 1. I suspect you want something like:
var sums = from p1 in dict1
where p1.Value != 0
join p2 in dict2 on p1.Key equals p2.Key
group p2 by p1.Value into g
select new { Key = g.Max(p2 => p2.Key),
Sum = g.Sum(p2 => p2.Value) };
That gives a result of:
4 74
7 35
It doesn't include your {1, 45} or {2, 30} results though. I thought you wanted to ignore entries in the first dictionary with a value of 0?
See more on this question at Stackoverflow