简体   繁体   English

使用Linq to XML和Distinct()的IEqualityComparer不在代码中执行?

[英]IEqualityComparer with Linq to XML and Distinct() is not executed in code?

It doesnt matter what I write in the Equals method. 我在Equals方法中写的并不重要。 The GetHashCode is always fired, but I do not know whose GetHashCode to return? GetHashCode总是被触发,但我不知道要返回哪个GetHashCode?

When the GetHashCode method is called then variable x has the following data: 调用GetHashCode方法时,变量x具有以下数据:

In the first unitName elementName is the value "This is the value I want to compare" ... 在第一个unitName中,elementName是值“这是我要比较的值”...

<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();
    }
}

It would make sense to return the hash code of the Value of the element as you are using that to determine equality. 当您使用它来确定相等时,返回元素Value的哈希码是有意义的。 Your GetHashCode() implementation must be consistent with your Equals() implementation. 您的GetHashCode()实现必须与您的Equals()实现一致。

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();
    }
}

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();
            }
        }

You can also write something which will work for most of xml. 您还可以编写适用于大多数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(); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM