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 nulls 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

Popular posts from this blog

jQuery clickable div with working mailto link inside -

java - Getting corefrences with Standard corenlp package -

WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl -