我有一个非常简单的测试方法,它返回一个包含许多重复项的列表,但是当它没有时我想我会尝试 HashSet 因为它应该删除重复项,但看来我需要覆盖 Equals 和 GetHashCode 但我是真的很难理解我需要做什么。我会很感激一些指示。
HashSet<object> test = XmlManager.PeriodHashSet(Server.MapPath("../Xml/XmlFile.xml"));
foreach (Object period in test2)
{
PeriodData pd = period as PeriodData;
Response.Write(pd.PeriodName + "<br>");
}
我也尝试了以下
List<object> test = XmlManager.PeriodList(Server.MapPath("../Xml/XmlFile.xml"));
List<object> test2 = test.Distinct().ToList();
foreach (Object period in test2)
{
PeriodData pd = period as PeriodData;
Response.Write(pd.PeriodName + "<br>");
}
PeriodData 对象的 delcarewd 如下:
public class PeriodData
{
private int m_StartYear = -9999999;
private int m_EndYear = -9999999;
private string m_PeriodName = String.Empty;
public int StartYear
{
get { return m_StartYear; }
set { m_StartYear = value; }
}
public int EndYear
{
get { return m_EndYear; }
set { m_EndYear = value; }
}
public string PeriodName
{
get { return m_PeriodName; }
set { m_PeriodName = value; }
}
}
这是我要删除重复的返回的 PeriodName 。