c# - LINQ Max() with Nulls -
i have list contains bunch of points (with x , y component).
i want max x points in list, this:
double max = pointlist.max(p=> p.x);
the problem when have null in list instead of point. best way around issue?
well, filter them out:
pointlist.where(p => p != null).max(p => p.x)
on other hand, if want null
s treated though points having x-coordinate 0
(or similar), do:
pointlist.max(p => p == null ? 0 : p.x)
do note both techniques throw if sequence empty. 1 workaround (if desirable) be:
pointlist.defaultifempty().max(p => p == null ? 0 : p.x)
Comments
Post a Comment