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

How to use C# LINQ Union to get the Union of Custom list1 with list2

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

我按照 MSDN Link 完成了工作,但我仍然无法达到同样的效果。

以下是自定义代码class:-

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

我这样调用:-

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

请提出实现相同目标的适当方法!

您需要将 IEqualityComparer 的实例传递给 Union 方法。该方法有一个重载要传递给您的比较器。

最简单也是最丑陋的解决方案是

var comparer = new CustomFormat(null,0);

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

您在实施过程中犯了一些错误。您不应在您的类型 (CustomFormat) 上实现 IEqualityComparer 方法,而应在单独的 class 上实现,例如 CustomFormatComparer.

在您的类型 (CustomFormat) 上,您应该实施 IEquatable

这里的奇怪之处在于您的 class 实现了 IEqualityComparer<CustomClass> 而不是 IEquatable<CustomClass>。您 可以 传递 CustomClass 的另一个实例,它将用作比较器,但只使 CustomClass 实现 IEquatable<CustomClass> 会更惯用, 并覆盖 Equals(object).

IEquatable<T>IEqualityComparer<T>的区别在于IEquatable<T>表示"I know how to compare myself with another instance of T"而IEqualityComparer<T>表示"I know how to compare two instances of 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;
    }
}

Enumerable.Union 的文档错误(如评论中所述)现在无济于事。它目前指出:

The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer<T> generic interface.

它应该是这样的:

The default equality comparer, Default, is used to compare values when a specific IEqualityComparer<T> is not provided. If T implements IEquatable<T>, the default comparer will use that implementation. Otherwise, it will use the implementation of Equals(object).