这是我的课程。
public class Report
{
public string Name { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Lat { get; set; }
public string Long { get; set; }
public string Type { get; set; }
public DateTime CreateDate { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var y = obj as Report;
if (y == null)
return false;
return
this.Name == y.Name &&
this.Long == y.Long &&
this.Lat == y.Lat;
}
public override int GetHashCode()
{
return (this.Name + this.Long + this.Lat).GetHashCode();
}
}
这是我的代码,当我创建一个新的HashSet时,某些不唯一的值会刺入吗?有任何想法吗? 看来我的帖子大部分是代码,所以我需要添加更多详细信息。
我正在使用此方法创建在HashSet中传递的对象(这是一个仅用于测试目的的控制台应用程序,没什么花哨的东西)
static Report CreateReport(dynamic report)
{
var result = new Report();
result.City = report.city.ToString();
result.Name = report.name.ToString();
result.Country = report.country.ToString();
result.Long = report.@long.ToString();
result.Lat = report.lat.ToString();
result.Type = report.type.ToString();
result.CreateDate = DateTime.Now;
return result;
}
答案 0 :(得分:0)
您是否尝试过使用IEqualityComparer<T>
为T
的{{1}}?
Report
然后使用它实例化您的public class ReportComparer : IEqualityComparer<Report>
{
public override bool Equals(object obj)
{
if (obj == null)
return false;
var y = obj as Report;
if (y == null)
return false;
return
this.Name == y.Name &&
this.Long == y.Long &&
this.Lat == y.Lat;
}
public int GetHashCode(Report obj)
{
return (this.Name + this.Long + this.Lat).GetHashCode();
}
}
:
HashSet
文档: