自定义对象使用 except 未能使用 IEqualityComparer;

发布于 2024-10-26 14:42:41 字数 1464 浏览 1 评论 0原文

这是目标代码:

public class DlpItem : IEqualityComparer<DlpItem>
{   
    public string Text { get; set; }
    public int Id { get; set; }

    public DlpItem(int pId)
    {
        Text = string.Empty;
        Id = pId;
    }

    public override bool Equals(object obj)
    {
        return Id == (obj as DlpItem).Id;
    }

    public bool Equals(DlpItem a, DlpItem b)
    {
        return a.Id == b.Id;
    }

    public int GetHashCode(DlpItem item)
    {
        return Id.GetHashCode();
    }
}

我有两个列表,如下所示:

var list1 = new List<DlpItem>();
list1.Add(new DlpItem(1));
list1.Add(new DlpItem(2));


var list2 = new List<DlpItem>();
list2.Add(new DlpItem(1));
list2.Add(new DlpItem(2));

var delItems = list1.Except(list2).ToList<DlpItem>();

delItems 中始终包含这两个项目。我在这里缺少什么?

编辑:代码现在实现 IEquatable

public class DlpItem : IEqualityComparer<DlpItem>, IEquatable<DlpItem>
{   
    public string Text { get; set; }
    public int Id { get; set; }


    public override bool Equals(object obj)
    {
        return Id - (obj as DlpItem).Id == 0;
    }

    public bool Equals(DlpItem a, DlpItem b)
    {
        return a.Id == b.Id;
    }

    public bool Equals(DlpItem item)
    {
        return item != null && Id == item.Id;
    }

    public int GetHashCode(DlpItem item)
    {
        return Id.GetHashCode();
    }
}

here is the object code:

public class DlpItem : IEqualityComparer<DlpItem>
{   
    public string Text { get; set; }
    public int Id { get; set; }

    public DlpItem(int pId)
    {
        Text = string.Empty;
        Id = pId;
    }

    public override bool Equals(object obj)
    {
        return Id == (obj as DlpItem).Id;
    }

    public bool Equals(DlpItem a, DlpItem b)
    {
        return a.Id == b.Id;
    }

    public int GetHashCode(DlpItem item)
    {
        return Id.GetHashCode();
    }
}

And I have two lists as follows:

var list1 = new List<DlpItem>();
list1.Add(new DlpItem(1));
list1.Add(new DlpItem(2));


var list2 = new List<DlpItem>();
list2.Add(new DlpItem(1));
list2.Add(new DlpItem(2));

var delItems = list1.Except(list2).ToList<DlpItem>();

delItems always has both items in it. What am I missing here?

EDIT: Code now implements IEquatable

public class DlpItem : IEqualityComparer<DlpItem>, IEquatable<DlpItem>
{   
    public string Text { get; set; }
    public int Id { get; set; }


    public override bool Equals(object obj)
    {
        return Id - (obj as DlpItem).Id == 0;
    }

    public bool Equals(DlpItem a, DlpItem b)
    {
        return a.Id == b.Id;
    }

    public bool Equals(DlpItem item)
    {
        return item != null && Id == item.Id;
    }

    public int GetHashCode(DlpItem item)
    {
        return Id.GetHashCode();
    }
}

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

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

发布评论

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

评论(1

临走之时 2024-11-02 14:42:41

在您的示例中,您实际上没有向 list2 添加任何内容...这是一个足够简单的错误,但有一个更重要的问题:

它需要是 IEquatable 不是 IEqualityComparer;另外,您可能想确保哈希码不能更改;最简单的方法是将 Id 设置为只读:

public class DlpItem : IEquatable<DlpItem>
{
    public string Text { get; set; }
    private readonly int id;
    public int Id { get { return id; } }

    public DlpItem(int id)
    {
        Text = "";
        this.id = id;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as DlpItem);
    }

    public bool Equals(DlpItem other)
    {
        return other != null && this.Id == other.Id;
    }
    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }
}

In your example, you don't actually add anything to list2... a simple enough mistake, but there is a more significant issue:

It needs to be IEquatable<T> not an IEqualityComparer<T>; also, you might want to ensure the hashcode can't change; most simply by making Id read-only:

public class DlpItem : IEquatable<DlpItem>
{
    public string Text { get; set; }
    private readonly int id;
    public int Id { get { return id; } }

    public DlpItem(int id)
    {
        Text = "";
        this.id = id;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as DlpItem);
    }

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