我正在尝试为我的对象实现一个 IEqualityComparer,它基本上可以检测一个对象是否比另一个对象旧。以下更简单的示例将综合我要完成的工作:
class Program
{
static void Main(string[] args)
{
var authorsList = new List<Author>()
{
new Author{ Firstname = "Bob", Lastname = "Smith", Age=11 },
new Author{ Firstname = "Bob", Lastname = "Smith", Age=20 },
new Author{ Firstname = "Bob", Lastname = "Smith", Age=12 },
new Author{ Firstname = "Bob", Lastname = "Smith", Age=14 },
new Author{ Firstname = "Bob", Lastname = "Smith", Age=12 },
new Author{ Firstname = "Fred", Lastname = "Smith", Age=12 },
new Author{ Firstname = "Trevor", Lastname = "Smith", Age=15 },
new Author{ Firstname = "Brian", Lastname = "Smith", Age=11 },
new Author{ Firstname = "Billy", Lastname = "Smith", Age=11 },
};
var authorsListExcept = new List<Author>()
{
new Author{ Firstname = "Bob", Lastname = "Smith", Age=12 },
new Author{ Firstname = "Fred", Lastname = "Smith", Age=12 },
};
var authorsList2 = authorsList
.Except(authorsListExcept, new AuthorUpdatedComparer()).ToList();
}
}
class Author
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public int Age { get; set; }
}
class AuthorUpdatedComparer : IEqualityComparer<Author>
{
public bool Equals(Author x, Author y)
{
return x.Age >= y.Age;
}
public int GetHashCode(Author obj)
{
//Check whether the object is null
if (Object.ReferenceEquals(obj, null)) return 0;
int FirstnameHash = (obj.Firstname ?? "").GetHashCode();
int LastnameHash = (obj.Lastname ?? "").GetHashCode();
int finalResult = FirstnameHash ^ LastnameHash;
return finalResult;
}
}
我的 authorsList2 结果是:* Bob Smith 20 岁 * Bom Smith 14 岁 * Trevor Smith 15 岁 * Brian Smith 11 岁 * Billy Smith 11 岁
但取而代之的是 14 岁的 Bob Smith 不包括在内。在调试时,我得出的结论是,包含 Bob Smith 和 20 岁开始使用它的比较器有一个比较器,排除了所有 Bob 小于 20 岁的比较器。
在我看来,这是一种奇怪的行为,它应该只排除那些年龄较小或与作者列表中包含的人相同年龄的人。我试图阅读 msdn 文档,我希望它应该发生:字体: http: //msdn.microsoft.com/en-us/library/bb336390 (v=vs.100).aspx
任何人都可以帮助我吗?谢谢,雨果萨尔加多