我使用Enumerable.Union<TSource>
方法来获取自定义列表1与自定义列表2的联合。但不知何故,它并没有像它应该在我的情况下工作。我把所有的项目都取了一遍。
我遵循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;
}
}
我打的电话如下:-
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<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;
}
}
现在它没有帮助(如注释所述)Enumerable.Union
的文档是错误的。它当前的状态是:
默认的相等比较器
Default
,用于比较实现IEqualityComparer<T>
泛型接口的类型的值。
应该是这样的:
默认的相等比较器
Default
,用于在没有指定IEqualityComparer<T>
的情况下比较值。如果T
实现了IEquatable<T>
,默认比较器将使用该实现。否则,它将使用Equals(object)
的实现。
您需要将IEqualityComparer
的实例传递给Union
方法。该方法有一个重载要传递给比较器。
最简单和最丑陋的解决方案是
var comparer = new CustomFormat(null,0);
l1 = l1.Union(l2, comparer).ToList();
你在执行中犯了一些错误。您不应该在您的类型(CustomFormat
)上实现IEqualityComparer
方法,而应该在单独的类(如CustomFormatComparer
)上实现。
在您的类型(CustomFormat
)中,您应该实现IEquatable
。