-1
var num = new List<double?>{null};  
var result = num.Sum(d => (double?)d);
Console.WriteLine(result is null);

How to make this return true?

Joe Riggs
  • 1,312
  • 4
  • 23
  • 48

1 Answers1

0

I'm don't think it is possible in "one go", but you can do something like this to get the desired result:

double? sum = d.Any((x) => x == null) ? null : d.Sum();
Tack D.
  • 157
  • 1
  • 6
  • 2
    The question is well-written but I interpreted as meaning if the list ONLY contains `null` then the sum should be `null`. In that case, you'd have to invert your check to look for any items that weren't `null` and switch the outputs. – jmcilhinney Jun 07 '23 at 12:58