使用HashSet删除重复项

时间:2011-09-19 22:35:16

标签: c# hashset

如何使用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; } 
    }

4 个答案:

答案 0 :(得分:3)

有几种方法可以做到这一点。

一种方法是覆盖Equals的{​​{1}}方法。请参阅Guidelines for Overriding Equals and Operator ==

另一种方法是创建一个DropDownListClass派生类进行比较:

EqualityComparer

当您创建public DropDownComparer : EqualityComparer<DropDownListClass> { public override bool Equals(DropDownListClass i1, DropDownListClass i2) { bool rslt = (i1.Id == i2.Id) && (i1.Name == i2.Name); } public override int GetHashCode(DropDownListClass x) { return x.Id.GetHashCode() ^ x.Id.GetHashCode(); } }

HashSet

告诉DropDownComparer comparer = new DropDownComparer(); HashSet<DropDownListClass> hashSetTopics = new HashSet<DropDownListClass>(comparer); 代码,“使用我的自定义比较函数来比较项目。”你的第一个循环应该可以正常工作。

答案 1 :(得分:2)

IComparible接口(IEqualityComparer):http://msdn.microsoft.com/en-us/library/system.icomparable.aspx

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

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();
        }
    }

答案 2 :(得分:1)

IList<ListItem> dropDownListItems = new List<ListItem>();

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

DropDownList1.DataSource = dropDownListItems.Distinct(); 
DropDownList1.DataBind();

修改 我认为你的HashSet中有重复,因为你的DropDownListClass没有实现IComparable

下面是重复数据如何进入HashSet的示例。 MyObject类可能应该实现interompace IComparible。

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;}
}

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

答案 3 :(得分:0)

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

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();

你也可以在DropDownListClass上实现一个基于Id属性进行比较的Equals方法。

最后,DropDownListClass可能有更好的名称,例如Topic。