c# - Get Distinct values out of List<Object> -
i have list
contains instances of beam
class. each of these beam
objects has elevation
property.
list<beam> beams = new list<beam> {beam1, beam2, ...}; public class beam { public double elevation; }
now want create list<double>
contains distinct elevations. example how write method accepts beams list below
var beam1 = new beam { elevation = 320); var beam2 = new beam { elevation = 320); var beam3 = new beam { elevation = 640); var beam4 = new beam { elevation = 0); list<beam> beams = new list<beam> {beam1, beam2, beam3, beam4};
and gives removing duplicate elevations:
listofelevations = {0, 320,640}
1) make beam implement icomparable:
public class beam : icomparable { public double elevation; //consider changing property, btw. public int compareto(object obj) { if (obj == null) return 1; beam otherbeam = obj beam; return this.elevation.compareto(otherbeam.elevation); } }
2) use distinct():
var listofelevations = beams.distinct().select(x=> x.elevation).tolist();
Comments
Post a Comment