LINQ Max() with Nulls

后端 未结 8 1639
自闭症患者
自闭症患者 2020-12-29 00:49

I have a list that contains a bunch of Points (with an X and Y component).

I want to get the Max X for all points in the list, like this:

double max          


        
8条回答
  •  礼貌的吻别
    2020-12-29 01:32

    Place a nullable cast INSIDE the expression to ensure that a blank list will cast as a null. You can then add defaults.

    double max = pointList.Max(p=>(double?)p.X) ?? 0;
    

提交回复
热议问题