c# - Return the element of a list which fulfills a certain condition -
i have class:
class point { double x, y; }
from list<point>
, want point
point.x + point.y
maximum in list. how in linq?
this 1 way (though not optimal means):
list<point> list = ...; point maxpoint = list.orderbydescending(p => p.x + p.y).first();
another way should perform better, involve modifying point
class implement icomparable<t>
, this:
class point : icomparable<point> { double x, y; public int compareto(point other) { return (x + y).compareto(other.x + other.y); } }
... allow do:
list<point> list = ...; point maxpoint = list.max();
Comments
Post a Comment