How to handle nulls in LINQ when using Min or Max?

后端 未结 5 1401
暖寄归人
暖寄归人 2020-12-14 05:48

I have the following Linq query:

result.Partials.Where(o => o.IsPositive).Min(o => o.Result)

I get an exception when result.Parti

5条回答
  •  自闭症患者
    2020-12-14 06:39

    Yet another way to express it in LINQ is to use Aggregate:

    var min = result.Partials
        .Where(o => o.IsPositive)
        .Select(o => o.Result)
        .Aggregate(0, Math.Min); // Or any other value which should be returned for empty list
    

提交回复
热议问题