我有一个包含组合对标识符的表格,我用它来浏览 CSV 文件以寻找匹配项。我将未识别的对捕获在列表中,并将它们发送到输出框以供以后添加。我希望输出只出现一次唯一对。该类声明如下:
public class Unmatched:IComparable<Unmatched>
{
public string first_code { get; set; }
public string second_code { get; set; }
public int CompareTo(Unmatched other)
{
if (this.first_code == other.first_code)
{
return this.second_code.CompareTo(other.second_code);
}
return other.first_code.CompareTo(this.first_code);
}
}
关于上面代码的一个注意事项:这将以相反的字母顺序返回它,要按字母顺序获取它,请使用以下行:
return this.first_code.CompareTo(other.first_code);
这是添加它的代码。这是在与数据表元素比较之后直接进行的
unmatched.Add(new Unmatched()
{ first_code = fields[clients[global_index].first_match_column]
, second_code = fields[clients[global_index].second_match_column] });
我想从列表中删除第一个代码和第二个代码都相等的所有对,即;
PTC,138A
PTC,138A
PTC,138A
MA9,5A
MA9,5A
MA9,5A
MA63,138A
MA63,138A
MA59,87BM
MA59,87BM
应该变成:
PTC, 138A
MA9, 5A
MA63, 138A
MA59, 87BM
我已尝试添加我自己的 Equate 和 GetHashCode,如下所述: http://www.morgantechspace.com/2014/01/Use-of-Distinct-with-Custom-Class-objects-in-C-Sharp.html
我试过的 SE 链接在这里:
How would I distinct my list of key/value pairs
Get list of distinct values in List<T> in c#
Get a list of distinct values in List
它们都返回一个仍然包含所有对的列表。这是输出列表的当前代码(是的,我知道有两条不同的线,但似乎都不起作用):
parser.Close();
List<Unmatched> noDupes = unmatched.Distinct().ToList();
noDupes.Sort();
noDupes.Select(x => x.first_code).Distinct();
foreach (var pair in noDupes)
{
txtUnmatchedList.AppendText(pair.first_code + "," + pair.second_code + Environment.NewLine);
}
这是请求的等式/哈希代码:
public bool Equals(Unmatched notmatched)
{
//Check whether the compared object is null.
if (Object.ReferenceEquals(notmatched, null)) return false;
//Check whether the compared object references the same data.
if (Object.ReferenceEquals(this, notmatched)) return true;
//Check whether the UserDetails' properties are equal.
return first_code.Equals(notmatched.first_code) && second_code.Equals(notmatched.second_code);
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public override int GetHashCode()
{
//Get hash code for the UserName field if it is not null.
int hashfirst_code = first_code == null ? 0 : first_code.GetHashCode();
//Get hash code for the City field.
int hashsecond_code = second_code.GetHashCode();
//Calculate the hash code for the GPOPolicy.
return hashfirst_code ^ hashsecond_code;
}
我还查看了一些使用查询和元组的答案,老实说我不明白。有人能给我指出一个来源或答案来解释如何(以及为什么)从自定义列表中获取不同的对吗?
(附带问题 - 你可以将一个类声明为 IComparable 和 IEquatable 吗?)
请您参考如下方法:
问题是你没有实现 IEquatable<Unmatched> .
public class Unmatched : IComparable<Unmatched>, IEquatable<Unmatched>
EqualityComparer<T>.Default使用 Equals(T)方法只有当你实现 IEquatable<T> .你没有这样做,所以它会改为使用 Object.Equals(object)它使用引用相等性。
Distinct 的过载你打电话使用 EqualityComparer<T>.Default 比较序列的不同元素是否相等。如文档所述,返回的比较器使用您对 GetHashCode 的实现找到可能相等的元素。然后它使用 Equals(T)检查相等性的方法,或 Object.Equals(Object)如果你还没有实现 IEquatable<T> .
你有一个 Equals(Unmatched)方法,但它不会被使用,因为你没有实现 IEquatable<Unmatched> .相反,默认 Object.Equals使用引用相等性的方法。
记下您当前的 Equals方法没有覆盖 Object.Equals因为这需要 Object参数,您需要指定 override修饰符。
