c# - Remove duplicates in a list of XYZ points -


mylist.groupby(x => new{x.x, x.y}).select(g => g.first()).tolist<xyz>(); 

the above code works fine me. want compare points based on round(5) of point component.

for example x.x = 16.838974347323224 should compared x.x = 16.83897 because experienced inaccuracy after round 5. suggestions?

solution:

mylist.groupby(x => new { x = math.round(x.x,5), y = math.round(x.y,5) })                .select(g => g.first()).tolist(); 

to use math.round:

var result = mylist.groupby(x => new { x = math.round(x.x,5, midpointrounding.awayfromzero), y = math.round(x.y,5, midpointrounding.awayfromzero) })                    .select(g => g.first()).tolist(); 

however if want remove duplicates instead of groupby go 1 of these:

  1. select rounded , distinct:

    var result = mylist.select(item => new xyz { x = math.round(item.x,5, midpointrounding.awayfromzero),                                               y = math.round(item.y,5, midpointrounding.awayfromzero)})                    .distinct().tolist(); 
  2. distinct , override equals , gethashcode - (equals rounding) - wouldn't suggest

  3. distinct , implement custom iequalitycomparer:

    public class roundedxyzcomparer : iequalitycomparer<xyz> {     public int roundingdigits { get; set; }     public roundedxyzcomparer(int roundingdigits)     {        roundingdigits = roundingdigits;     }      public bool equals(xyz x, xyz y)     {        return math.round(x.x, roundingdigits, midpointrounding.awayfromzero) == math.round(y.x, roundingdigits, midpointrounding.awayfromzero) &&                math.round(x.y,roundingdigits, midpointrounding.awayfromzero) == math.round(y.y, roundingdigits, midpointrounding.awayfromzero);     }      public int gethashcode(xyz obj)     {        return math.round(obj.x, roundingdigits, midpointrounding.awayfromzero).gethashcode() ^               math.round(obj.y, roundingdigits, midpointrounding.awayfromzero).gethashcode();     }  }   //use:  mylist.distinct(new roundedxyzcomparer(5)); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -