3

It doesnt matter what I write in the Equals method. The GetHashCode is always fired, but I do not know whose GetHashCode to return?

When the GetHashCode method is called then variable x has the following data:

In the first unitName elementName is the value "This is the value I want to compare" ...

<unit>
  <unitName>This is the value I want to compare</unitName>
  <units>
    <unit>
      <unitName>xxx</unitName>      
      <units>
        <unit>
          <unitName>cccc</unitName>
          <test>33</test>
          <test>44</test>                   
        </unit>
      </units>          
        </unit>
    </units>        
</unit>
IEnumerable<XElement> tempMemberList = doc.Elements("dep").Descendants("customers").Distinct(new XElementComparer());

public class XElementComparer : IEqualityComparer<XElement> {
    public bool Equals(XElement x, XElement y) {

        return x.Value == y.Value;
    }

    public int GetHashCode(XElement x) {
        return x.GetHashCode();
    }
}
svick
  • 236,525
  • 50
  • 385
  • 514
msfanboy
  • 5,273
  • 13
  • 69
  • 120

3 Answers3

3

It would make sense to return the hash code of the Value of the element as you are using that to determine equality. Your GetHashCode() implementation must be consistent with your Equals() implementation.

public class XElementComparer : IEqualityComparer<XElement> {
    public bool Equals(XElement x, XElement y) {
        return x.Value == y.Value;
    }

    public int GetHashCode(XElement x) {
        return x.Value.GetHashCode();
    }
}
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • p.s., Your query doesn't actually yield any elements. There aren't any `dep` elements nor are there `customers` elements in your posted XML. – Jeff Mercado Oct 17 '11 at 19:09
  • Well sure that's fine, but if you're going to change the names, you should be consistent and change all its uses so you don't break anything. ;) – Jeff Mercado Oct 17 '11 at 21:34
  • As you knew my xml structure you should have used "unitName" :) – msfanboy Oct 25 '11 at 15:02
1

This is the solution, I just had to get the proper value from the first unitName I wanted...

public class XElementComparer : IEqualityComparer<XElement>
        {
            public bool Equals(XElement x, XElement y)
            {
                string unitNameX = x.Element("unitName ").Value;
                string unitNameY = y.Element("unitName ").Value;
                return unitNameX == unitName Y;
            }

            public int GetHashCode(XElement x)
            {
                string val = x.Element("unitName ").Value;
                return val.GetHashCode();
            }
        }
msfanboy
  • 5,273
  • 13
  • 69
  • 120
0

You can also write something which will work for most of xml.

public class XElementComparer : IEqualityComparer<XElement>
{
    public bool Equals(XElement x, XElement y) 
    {
        return (x.FirstAttribute.Value.Equals(y.FirstAttribute.Value) 
                && x.LastAttribute.Value.Equals(y.LastAttribute.Value)); 
    } 

    public int GetHashCode(XElement x) 
    { 
        return x.Value.GetHashCode(); 
    }
}
Rajnikant
  • 2,176
  • 24
  • 23