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

后端 未结 5 1405
暖寄归人
暖寄归人 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:29

    You can use the DefaultIfEmpty method to ensure the collection has at least 1 item:

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

提交回复
热议问题