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
Well, you could just filter them out:
pointList.Where(p => p != null).Max(p => p.X)
On the other hand, if you want null
s to be treated as though they were points having X-coordinate 0
(or similar), you could do:
pointList.Max(p => p == null ? 0 : p.X)
Do note that both techniques will throw if the sequence is empty. One workaround for this (if desirable) would be:
pointList.DefaultIfEmpty().Max(p => p == null ? 0 : p.X)
If you want to provide a default value for X of a null point:
pointList.Max(p => p == null ? 0 : p.X)
Or to provide a default for an empty list:
int max = points.Where(p => p != null)
.Select(p => p.X)
.DefaultIfEmpty()
.Max();