如何使用C#LINQ Union获取自定义list1与list2的联合

JDo*_*shi 8 c# linq union extension-methods

我正在使用该Enumerable.Union<TSource>方法来获取Custom List1与Custom List2的并集.但不知怎的,它在我的情况下不起作用.我得到的所有物品也重复一次.

我按照MSDN链接完成了工作,但我仍然无法实现相同的目标.

以下是自定义类的代码: -

public class CustomFormat : IEqualityComparer<CustomFormat>
{
    private string mask;

    public string Mask
    {
        get { return mask; }
        set { mask = value; }
    }

    private int type;//0 for Default 1 for userdefined

    public int Type
    {
         get { return type; }
         set { type = value; }
    }
    public CustomFormat(string c_maskin, int c_type)
    {
        mask = c_maskin;
        type = c_type;
    }

    public bool Equals(CustomFormat x, CustomFormat y)
    {
        if (ReferenceEquals(x, y)) return true;

        //Check whether the products' properties are equal. 
        return x != null && y != null && x.Mask.Equals(y.Mask) && x.Type.Equals(y.Type);
    }

    public int GetHashCode(CustomFormat obj)
    {
        //Get hash code for the Name field if it is not null. 
        int hashProductName = obj.Mask == null ? 0 : obj.Mask.GetHashCode();

        //Get hash code for the Code field. 
        int hashProductCode = obj.Type.GetHashCode();

        //Calculate the hash code for the product. 
        return hashProductName ^ hashProductCode;
    }
}
Run Code Online (Sandbox Code Playgroud)

我打电话如下: -

List<CustomFormat> l1 = new List<CustomFormat>();
l1.Add(new CustomFormat("#",1));
l1.Add(new CustomFormat("##",1));
l1.Add(new CustomFormat("###",1));
l1.Add(new CustomFormat("####",1));

List<CustomFormat> l2 = new List<CustomFormat>();
l2.Add(new CustomFormat("#",1));
l2.Add(new CustomFormat("##",1));
l2.Add(new CustomFormat("###",1));
l2.Add(new CustomFormat("####",1));
l2.Add(new CustomFormat("## ###.0",1));

l1 = l1.Union(l2).ToList();

foreach(var l3 in l1)
{
    Console.WriteLine(l3.Mask + " " + l3.Type);
}
Run Code Online (Sandbox Code Playgroud)

请建议实现相同的适当方式!

Jon*_*eet 8

这里奇怪的是你的类实现IEqualityComparer<CustomClass>而不是IEquatable<CustomClass>.您可以传入另一个CustomClass将用作比较器的实例,但只是制作CustomClass工具IEquatable<CustomClass>,并且也会覆盖更为惯用Equals(object).

之间的区别IEquatable<T>,并IEqualityComparer<T>IEquatable<T>说:"我知道如何用自己的另一个实例比较T",而IEqualityComparer<T>说"我知道如何比较的两个实例T".后者通常是单独提供的 - 就像它可以Union通过另一个参数提供一样.类型实现IEqualityComparer<T>其自己的类型是非常罕见的- 而IEquatable<T>应该用于比较相同类型的值.

这是一个使用自动实现的属性以实现简单性和更多惯用参数名称的实现.我可能会自己更改哈希代码实现并使用表达式身体成员,但这是另一回事.

public class CustomFormat : IEquatable<CustomFormat>
{
    public string Mask { get; set; }
    public int Type { get; set; }

    public CustomFormat(string mask, int type)
    {
        Mask = mask;
        Type = type;
    }

    public bool Equals(CustomFormat other)
    {
        if (ReferenceEquals(this, other))
        {
            return true;
        }
        return other != null && other.Mask == Mask && other.Type == Type;
    }

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

    public override int GetHashCode()
    {
        // Get hash code for the Name field if it is not null. 
        int hashProductName = Mask == null ? 0 : Mask.GetHashCode();

        //Get hash code for the Code field. 
        int hashProductCode = Type.GetHashCode();

        //Calculate the hash code for the product. 
        return hashProductName ^ hashProductCode;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在它没有帮助(如评论中所述)文档Enumerable.Union是错误的.它目前指出:

默认的相等比较器Default用于比较实现IEqualityComparer<T>通用接口的类型的值.

它应该说:

默认的相等比较器Default用于比较IEqualityComparer<T>未提供特定值时的值.如果是Timplements IEquatable<T>,则默认比较器将使用该实现.否则,它将使用执行Equals(object).