Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
4.43/5 (3 votes)
See more:
Hello Guys,
I was wondering if anyone knows a better way to get a distinct from List<some_class> not using any helper classes.

Currently I have this (I have simplified this):
C#
class A
{
int X, Y;
public A(int xx, int yy)
{ X = xx; Y = yy;}
}

//this is a helper class I want to get rid of
class helper : IEqualityComparer<A>
{
public bool Equals(A x, A y)
{return x.X == y.X && x.Y == y.Y;}

public int GetHashCode(A obj)
{return 0;}
}

List<A> class_array = new List<A>
class_array.Add(new A(1, 1));
class_array.Add(new A(1, 1));
class_array.Add(new A(2, 2));
class_array.Add(new A(2, 2));

class_array = class_array.Distinct(new helper()).ToList()


Anyone know a better solution for the situation? The reason I need this is that I have multiple class arrays to compare and I dont really want to have helper comparison classes for each... This just does not sound right...

Thank you in advance!
Posted

From the documentation[^]:

The default equality comparer, Default, is used to compare values of the types that implement the IEquatable<t>[^] generic interface. To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type.

So class A would need to implement this interface.


-Edit-
This works fine for me:
C#
class A : IEquatable<A>
{
	int X, Y;
 
	public A(int xx, int yy) { X = xx; Y = yy; }	
	public override bool Equals(object other) { return Equals((A)other); }	 // Note that this implementation doesn't follow proper standards. Always check for null values, or objects of a different type and handle them accordingly.
	public bool Equals(A other) { return X == other.X && Y == other.Y; }	
	public override int GetHashCode() { return (X + Y).GetHashCode(); }
}
 
Share this answer
 
v3
Comments
MK-Gii 5-Feb-14 4:03am    
Just checked on MSDN documentation. Looks like this interface has only 1 method in it's signature - Equals... no "GetHashCode"..
Nevertheless - I have tried this an it does not really work. My question might not be obvious but I am trying to use LINQ for filtration and I need the LINQ to be able to distinguish between similar classes.
Maarten Kools 5-Feb-14 4:31am    
When you implement the Equals method it is apparently important to implement the GetHashCode as well. More info on that can be found here[^]
MK-Gii 5-Feb-14 4:40am    
I see the idea behind it now.... thanks!
Maarten Kools 5-Feb-14 4:38am    
I've added a sample, and that works fine for me. Maybe you didn't implement it right?
Try is as below.

Note:I didn't test.Please check that.

XML
List<A> distinct =
  class_array
  .GroupBy(a => new {a.X,a.Y})
  .Select(g => g.First())
  .ToList();
 
Share this answer
 
Comments
MK-Gii 5-Feb-14 4:39am    
That worked. Thanks.
Sampath Lokuge 5-Feb-14 4:52am    
Glad to hear that it helped! :)
Just for variety:
C#
List<A> ADeDupe = class_array
    .Select(a => new {X = a.X, Y = a.Y})
        .Distinct()
            .ToList()
                .Select(a => new A(a.X, a.Y))
                    .ToList();
imho, your code will be more maintainable in the future if you implement the custom Comparer, and simply use 'Distinct, rather than using "tricky" Linq.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900