带有 Linq to XML 和 Distinct() 的 IEqualityComparer 未在代码中执行?

发布于 2024-12-10 16:20:43 字数 1045 浏览 0 评论 0原文

我在 Equals 方法中写什么并不重要。 GetHashCode总是被触发,但我不知道GetHashCode返回谁?

当调用 GetHashCode 方法时,变量 x 具有以下数据:

在第一个 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 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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只有影子陪我不离不弃 2024-12-17 16:20:43

当您使用元素的 Value 的哈希码来确定相等性时,返回它是有意义的。您的 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();
    }
}

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();
    }
}
流星番茄 2024-12-17 16:20:43

这就是解决方案,我只需从我想要的第一个 unitName 中获取正确的值......

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

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();
            }
        }
预谋 2024-12-17 16:20:43

您还可以编写适用于大多数 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(); 
    }
}

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(); 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文