ivan.petrovic ivan.petrovic - 3 months ago 10
C# Question

Implement IEqualityComparer

I would like to distinct object from list. I try to implement IEqualityComparer but without success. Please if somebody can review my code and give me some explain about IEqualityComparer. I have following situation:

public class Message
{
public int x { get; set; }
public string y { get; set; }
public string z { get; set; }
public string w { get; set; }
}

public class MessageComparer : IEqualityComparer<Message>
{
public bool Equals(Message x, Message y)
{
if (Object.ReferenceEquals(x, y)) return true;

if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
if (x.x == y.x && x.y == y.y && x.z == y.z && x.w == y.w)
{
return true;
}
return false;
}

public int GetHashCode(Message number)
{
//if (Object.ReferenceEquals(number, null)) return 0;
int hashX = number.x.GetHashCode();
int hashY = number.y == null ? 0 : number.y.GetHashCode();
int hashZ = number.z == null ? 0 : number.z.GetHashCode();
int hashW = number.w == null ? 0 : number.w.GetHashCode();

return hashX ^ hashY ^ hashZ ^ hashW;

}
}


This is my list with Message objects:

Message m1 = new Message();
m1.x = 1;
m1.y = "A";
m1.z = "B";
m1.w = "C";

Message m2 = new Message();
m2.x = 1;
m2.y = "A";
m2.z = "B";
m2.w = "C";

Message m3 = new Message();
m3.x = 1;
m3.y = "A";
m3.z = "B";
m3.w = "C";

Message m4 = new Message();
m4.x = 2;
m4.y = "A";
m4.z = "B";
m4.w = "C";

Message m5 = new Message();
m5.x = 3;
m5.y = "W";
m5.z = "D";
m5.w = "C";

Message m6 = new Message();
m6.x = 4;
m6.y = "S";
m6.z = "F";
m6.w = "R";

List<Message> collection = new List<Message>();
collection.Add(m1);
collection.Add(m2);
collection.Add(m3);
collection.Add(m4);
collection.Add(m5);

collection.Distinct(new MessageComparer());


When I call Distinct() method number of elements in list are same.
Thank you.

Answer

Try this:

var distinct = collection.Distinct(new MessageComparer());

Then use distinct for anything after that.

It looks like you're forgetting the immutable nature of IEnumerable<>. None of the LINQ methods actually change the original variable. Rather, they return IEnuerable<T>s which contain the result of the expression. For example, let's consider a simple List<string> original with the contents { "a", "a", "b", "c" }.

Now, let's call original.Add("d");. That method has no return value (it's void). But if we then print out the contents of original, we will see { "a", "a", "b", "c", "d" }.

On the other hand, let's now call original.Skip(1). This method does have a return value, one of type IEnumerable<string>. It is a LINQ expression, and performs no side-effecting actions on the original collection. Thus, if we call that and look at original, we will see { "a", "a", "b", "c", "d" }. However, the result from the method will be { "a", "b", "c", "d" }. As you can see, the result skips one element.

This is because LINQ methods accept IEnumerable<T> as a parameter. Consequently, they have no concept of the implementation of the original list. You could be passing, via extension method, a ReadOnlyCollection and they would still be able to evaluate through it. They cannot, then, alter the original collection, because the original collection could be written in any number of ways.

All that, but in table form. Each lines starts with the original { "a", "a", "b", "c" }:

Context     Example function    Immutable?    Returned Value     Collection after calling
Collection  Add("d")            No            (void)             { "a", "a", "b", "c", "d" }:
LINQ        Skip(1)             Yes           { "a", "b", "c" }  { "a, "a", "b", "c" }: