C# 使用HashSet删除重复项

C# 使用HashSet删除重复项,c#,hashset,C#,Hashset,如何使用HashSet删除重复项,或者如果您有更好的想法,请让我知道,但到目前为止,这是我正在做的 我试图消除重复和下面的代码是我正在使用 HashSet<DropDownListClass> hashSetTopics = new HashSet<DropDownListClass>(); foreach (XmlNode node in topicNodes) { string topicId = node.Attributes["TopicId"].Val

如何使用HashSet删除重复项,或者如果您有更好的想法,请让我知道,但到目前为止,这是我正在做的

我试图消除重复和下面的代码是我正在使用

HashSet<DropDownListClass> hashSetTopics = new HashSet<DropDownListClass>(); 

foreach (XmlNode node in topicNodes)
{
   string topicId = node.Attributes["TopicId"].Value;
   string topicName = node.Attributes["TopicName"].Value;
   hashSetTopics.Add(new DropDownListClass { Id = topicId, Name = topicName }); 
} 

the below code does removes the duplicates but the problem with the below is that i need a way to attach the id with name... at the end i am binding to dropdownlist.

HashSet<string> hashSetTopics1 = new HashSet<string>(); 

foreach (XmlNode node in topicNodes)
{
   string topicId = node.Attributes["TopicId"].Value;
   string topicName = node.Attributes["TopicName"].Value;
   hashSetTopics1.Add(topicName }); 
} 

DropDownList1.DataSource = hashSetTopics; /hashSetTopics1
DropDownList1.DataBind();


 public class DropDownListClass
    {
        public string Id { get; set; }
        public string Name { get; set; } 
    }
HashSet hashSetTopics=new HashSet();
foreach(topicNodes中的XmlNode节点)
{
字符串topicId=node.Attributes[“topicId”].Value;
字符串topicName=node.Attributes[“topicName”].Value;
Add(新的DropDownListClass{Id=topicId,Name=topicName});
} 
下面的代码确实删除了重复项,但下面的问题是我需要一种方法来附加带有名称的id。。。最后,我绑定到dropdownlist。
HashSet hashSetTopics1=新HashSet();
foreach(topicNodes中的XmlNode节点)
{
字符串topicId=node.Attributes[“topicId”].Value;
字符串topicName=node.Attributes[“topicName”].Value;
hashSetTopics1.Add(topicName});
} 
DropDownList1.DataSource=hashSetTopics/hashSetTopics1
DropDownList1.DataBind();
公共类DropDownListClass
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
}
IList dropDownListItems=new List();
foreach(topicNodes中的XmlNode节点)
{
字符串topicId=node.Attributes[“topicId”].Value;
字符串topicName=node.Attributes[“topicName”].Value;
添加(新列表项(topicName,topicId));
} 
DropDownList1.DataSource=dropDownListItems.Distinct();
DropDownList1.DataBind();
编辑 我认为您的HashSet中有DUP,因为DropDownList类没有实现IComparable

下面是一个重复数据如何进入哈希集的示例。MyObject类可能应该实现interface IComparable

public void Main()
{
    HashSet<MyObject> myObjects = new HashSet<MyObject>();
    bool success = myObjects.Add(new MyObject{ID = 1});
    success.Dump("First"); //Returns true

    success = myObjects.Add(new MyObject{ID = 1});

    success.Dump("Second"); //Returns true should have been false
}

// Define other methods and classes here
public class MyObject
{
    public int ID {get; set;}
}
public void Main()
{
HashSet myObjects=新HashSet();
bool success=myObjects.Add(新的MyObject{ID=1});
success.Dump(“First”);//返回true
success=myObjects.Add(新的MyObject{ID=1});
success.Dump(“Second”);//返回true应该是false
}
//在此处定义其他方法和类
公共类MyObject
{
公共int ID{get;set;}
}
您甚至可以从原始HashSet对象中调用Distinct(),但这只会隐藏它已损坏。

IList dropDownListItems=new List();
foreach(topicNodes中的XmlNode节点)
{
字符串topicId=node.Attributes[“topicId”].Value;
字符串topicName=node.Attributes[“topicName”].Value;
添加(新列表项(topicName,topicId));
} 
DropDownList1.DataSource=dropDownListItems.Distinct();
DropDownList1.DataBind();
编辑 我认为您的HashSet中有DUP,因为DropDownList类没有实现IComparable

下面是一个重复数据如何进入哈希集的示例。MyObject类可能应该实现interface IComparable

public void Main()
{
    HashSet<MyObject> myObjects = new HashSet<MyObject>();
    bool success = myObjects.Add(new MyObject{ID = 1});
    success.Dump("First"); //Returns true

    success = myObjects.Add(new MyObject{ID = 1});

    success.Dump("Second"); //Returns true should have been false
}

// Define other methods and classes here
public class MyObject
{
    public int ID {get; set;}
}
public void Main()
{
HashSet myObjects=新HashSet();
bool success=myObjects.Add(新的MyObject{ID=1});
success.Dump(“First”);//返回true
success=myObjects.Add(新的MyObject{ID=1});
success.Dump(“Second”);//返回true应该是false
}
//在此处定义其他方法和类
公共类MyObject
{
公共int ID{get;set;}
}

您甚至可以从原始HashSet对象中调用Distinct(),但这只是隐藏了它已损坏。

尝试使用字典-您可以根据键获得唯一性,值可以是任何值

Dictionary<string, DropDownListClass> topics = new Dictionary<string, DropDownListClass>(); 

foreach (XmlNode node in topicNodes)
{
   string topicId = node.Attributes["TopicId"].Value;
   string topicName = node.Attributes["TopicName"].Value;
   topics[topicName] = new DropDownListClass { Id = topicId, Name = topicName }; 
} 

DropDownList1.DisplayMember = "Name";
DropDownList1.ValueMember = "Id";
DropDownList1.DataSource = topics.Values;
DropDownList1.DataBind();
字典主题=新建字典();
foreach(topicNodes中的XmlNode节点)
{
字符串topicId=node.Attributes[“topicId”].Value;
字符串topicName=node.Attributes[“topicName”].Value;
topics[topicName]=newdropdownlistclass{Id=topicId,Name=topicName};
} 
DropDownList1.DisplayMember=“Name”;
DropDownList1.ValueMember=“Id”;
DropDownList1.DataSource=topics.Values;
DropDownList1.DataBind();
您还可能在DropDownListClass上实现一个Equals方法,该方法基于Id属性进行比较


最后,DropDownListClass可能有一个更好的名称,比如Topic。

尝试使用字典-您可以根据键获得唯一性,值可以是任何值

Dictionary<string, DropDownListClass> topics = new Dictionary<string, DropDownListClass>(); 

foreach (XmlNode node in topicNodes)
{
   string topicId = node.Attributes["TopicId"].Value;
   string topicName = node.Attributes["TopicName"].Value;
   topics[topicName] = new DropDownListClass { Id = topicId, Name = topicName }; 
} 

DropDownList1.DisplayMember = "Name";
DropDownList1.ValueMember = "Id";
DropDownList1.DataSource = topics.Values;
DropDownList1.DataBind();
字典主题=新建字典();
foreach(topicNodes中的XmlNode节点)
{
字符串topicId=node.Attributes[“topicId”].Value;
字符串topicName=node.Attributes[“topicName”].Value;
topics[topicName]=newdropdownlistclass{Id=topicId,Name=topicName};
} 
DropDownList1.DisplayMember=“Name”;
DropDownList1.ValueMember=“Id”;
DropDownList1.DataSource=topics.Values;
DropDownList1.DataBind();
您还可能在DropDownListClass上实现一个Equals方法,该方法基于Id属性进行比较

最后,DropDownList类可能有一个更好的名称,比如Topic。

IComparable接口(IEqualityComparer):

您可以将此代码用于通用相等投影

public sealed class ProjectionComparer<TValue, TProjection> : IEqualityComparer<TValue>
    {
        readonly Func<TValue, TProjection> _projection;

        public ProjectionComparer(Func<TValue, TProjection> projection)
        {
            projection.AssertParameterNotNull("projection");
            _projection = projection;
        }

        bool IEqualityComparer<TValue>.Equals(TValue x, TValue y)
        {
            var projectedX = _projection(x);
            var projectedY = _projection(y);

            return projectedX.Equals(projectedY);
        }

        int IEqualityComparer<TValue>.GetHashCode(TValue obj)
        {
            var projectedObj = _projection(obj);
            return projectedObj.GetHashCode();
        }
    }
公共密封类项目比较人:IEqualityComparer
{
只读函数投影;
公共项目比较人(Func项目)
{
projection.AssertParameterNotNull(“projection”);
_投影=投影;
}
布尔IEqualityComparer.Equals(t值x,t值y)
{
var projectedX=_投影(x);
var projectedY=_projection(y);
返回projectedX.Equals(projectedY);
}
int IEqualityComparer.GetHashCode(TValue obj)
{
var projectedObj=_projection(obj);
返回projectedObj.GetHashCode();
}
}
i兼容界面(IEqualityComparer):

您可以将此代码用于通用相等投影

public sealed class ProjectionComparer<TValue, TProjection> : IEqualityComparer<TValue>
    {
        readonly Func<TValue, TProjection> _projection;

        public ProjectionComparer(Func<TValue, TProjection> projection)
        {
            projection.AssertParameterNotNull("projection");
            _projection = projection;
        }

        bool IEqualityComparer<TValue>.Equals(TValue x, TValue y)
        {
            var projectedX = _projection(x);
            var projectedY = _projection(y);

            return projectedX.Equals(projectedY);
        }

        int IEqualityComparer<TValue>.GetHashCode(TValue obj)
        {
            var projectedObj = _projection(obj);
            return projectedObj.GetHashCode();
        }
    }
公共密封类项目比较人:IEqualityComparer
{
只读函数投影;
公共项目比较人(Func项目)
{
投影