IEqualityComparer 用于具有许多属性且没有唯一值的类

发布于 2024-09-06 09:08:57 字数 620 浏览 2 评论 0原文

这个类的 IEqualityComparer 的实现如何?

ID 属性不是唯一的。这两个属性都没有独特的值。

该实体有 7 个属性。

[Serializable()]
public class ServidorSeleccionadoDto
{
    [XmlAttribute()]
    public int Id { get; set; }

    [XmlAttribute()]
    public string Nombre { get; set; }

    [XmlAttribute()]
    public string IP { get; set; }

    [XmlAttribute()]
    public string Entorno { get; set; }

    [XmlAttribute()] // [XmlIgnore()]
    public string Habilitado { get; set; }

    [XmlAttribute()]
    public string Tipo { get; set; }

    [XmlAttribute()]
    public int IdGrupo { get; set; }
}

How do implementation for IEqualityComparer for this class?

The ID property is not unique. Neither properties has unique values.

The entity has 7 properties.

[Serializable()]
public class ServidorSeleccionadoDto
{
    [XmlAttribute()]
    public int Id { get; set; }

    [XmlAttribute()]
    public string Nombre { get; set; }

    [XmlAttribute()]
    public string IP { get; set; }

    [XmlAttribute()]
    public string Entorno { get; set; }

    [XmlAttribute()] // [XmlIgnore()]
    public string Habilitado { get; set; }

    [XmlAttribute()]
    public string Tipo { get; set; }

    [XmlAttribute()]
    public int IdGrupo { get; set; }
}

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

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

发布评论

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

评论(1

猥︴琐丶欲为 2024-09-13 09:08:57

看起来您的对象有一个 ID 字段。如果这对于每个对象都是唯一的,那么您只需要比较该属性。

public Boolean Equals(ServidorSeleccionadoDto obj)
{
   return this.ID == obj.ID;
}

注意:这仅在 ID 唯一的情况下才有效。


如果 ID 属性不唯一,您将需要比较所有对象属性。

public Boolean Equals(ServidorSeleccionadoDto obj)
{
    return this.ID == obj.ID && 
           this.Nombre.Equals(obj.Nombre) && 
           ... etc
}

It looks like your object has an ID field. If this is unique to each object then you would only need to compare on that property.

public Boolean Equals(ServidorSeleccionadoDto obj)
{
   return this.ID == obj.ID;
}

Note: This would only work if ID is unique.


If the ID property is not unique you will need to compare all your objects properties.

public Boolean Equals(ServidorSeleccionadoDto obj)
{
    return this.ID == obj.ID && 
           this.Nombre.Equals(obj.Nombre) && 
           ... etc
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文