我正在使用 Enumerable.Union<TSource>
获取 Custom List1 与 Custom List2 的并集的方法。但不知何故,它在我的情况下不起作用。我得到所有的项目也重复一次。
我关注了 MSDN Link完成工作,但我仍然无法实现同样的目标。
以下是自定义类的代码:-
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
的文档是错的。它目前指出:
The default equality comparer,
Default
, is used to compare values of the types that implement theIEqualityComparer<T>
generic interface.
它应该是这样的:
The default equality comparer,
Default
, is used to compare values when a specificIEqualityComparer<T>
is not provided. IfT
implementsIEquatable<T>
, the default comparer will use that implementation. Otherwise, it will use the implementation ofEquals(object)
.
关于c# - 如何使用 C# LINQ Union 获取自定义 list1 与 list2 的联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39851035/