The problem you are facing is operator priority. I think you want to treat a nullas 0. This will:
int? a = null;
int? b = 1;
int? c = 2;
int result = (a ?? 0) + (b ?? 0) + (c ?? 0);
What you wrote is equivalent
int result = a ?? (0 + b) ?? (0 + c) ?? 0;
So, all you have to do is change g.Sum(x => ...), and it should work as intended.
source
share