c# - 从 List<OtherObject> 创建 List<CustomObject>,删除重复项。

标签 c# linq

有一个非常相关的问题:Create List<CustomObject> from List<string>但是它不处理同时删除重复项。

我有以下类示例:

class Widget
{
    public string OwnerName;
    public int SomeValue;
}

class Owner
{
    public string Name;
    public string OtherData;
}

我想根据小部件列表创建一个所有者列表,但只有唯一的所有者名称。

这是我尝试过的:

List<Owner> Owners = MyWidgetList.Select(w => new Owner { Name = w.OwnerName }).Distinct().ToList();

问题是结果列表中有重复项。我做错了什么?

最佳答案

你需要定义GetHashCode()Equals()为您的对象定义自定义类型的相等性。否则它会根据引用本身进行比较。

这是因为 LINQ 扩展方法使用 IEqualityComparer比较对象的接口(interface)。如果你没有定义一个自定义比较器(你可以通过创建一个单独的类来实现 IEqualityComparer<Owner> ),它将使用默认的相等比较器,它使用类的 Equals()GetHashCode()定义。其中,如果您不覆盖它们,则会在 Equals() 上进行引用比较并返回默认的对象哈希码。

要么定义自定义IEqualityComparer<Owner> (因为您在 Owner 序列上调用 distinct)或添加 Equals()GetHashCode()为你的类(class)。

public class Owner
{
    public string Name;
    public string OtherData;

    public override Equals(object other)
    {
        if (ReferenceEquals(this, other))
            return true;

        if (other == null)
            return false;

        // whatever your definition of equality is...
        return Name == other.Name && OtherData == other.OtherData;
    }

    public override int GetHashCode()
    {
        int hashCode = 0;

        unchecked
        {
           // whatever hash code computation you want, but for example...
            hashCode += 13 * Name != null ? Name.GetHashCode() : 0;
            hashCode += 13 * OtherData != null ? OtherData.GetHashCode() : 0;
        }

        return hashCode;
    }
}

完成后,您编写的查询将正常工作。

关于c# - 从 List<OtherObject> 创建 List<CustomObject>,删除重复项。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6865153/

相关文章:

c# - 如何修改此正则表达式,使其只匹配目录并排除文件?

c# - Linq 实体,多对多和动态 where 子句

c# - 使用 LINQ 填充 DropDownList 的正确方法是什么?

c# - 如何将 "NULL/DbNull"发送到数据类型为 : nullable int 的表列中

c# - 如何在 EF 查询中执行日期比较?

c# - 私有(private)方法的命名约定 C# - Visual Studio 2017

c# - 在 Medium Trust 中调用内部方法

c# - 类名的 CompilerServices

c# - 单独组装和路由中的 Controller

linq - 这个 PredicateBuilder 类是如何工作的?