2011-09-19 21 views
0

如何刪除使用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; } 
    } 
+5

'從Hashset中刪除重複項。 Man,Hashset不能有重複,這就是它的重點。 –

+1

@Darin我想他是問如何使用Hashset刪除重複作爲一種做法,而不是從Hashset中刪除重複。 –

+1

但是他又一次從另一個哈希集合中做出來,所以我看到了這個問題。 :) –

回答

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

編輯 我覺得你得到的DUP在HashSet的,因爲你的DropDownListClass沒有實現IComparable的

下面是重複數據如何進入HashSet的示例。 MyObject類應該可能實現interomace 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的對象,但只是隱藏了它的破碎。

+0

第一種方法是擺脫愚蠢,但問題是,我如何分配'DataTextField'和'DataTextValue'到dropdownlsit? –

+0

我不知道我是否理解你的第二個方法'MyObject',它將如何與真實的數據場景一起工作 –

+0

@Abu我舉了一個例子,說明爲什麼你的HashSet沒有像它應該的那樣有不同的值。MyObject類沒有辦法比較它的self,所以它被添加到HashSet中,就好像它不是重複的一樣,這就是爲什麼在上面的問題中,重複項從已經不應該有重複項的對象中被移除。 –

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

你也很可能實現等號,相比基於ID屬性上DropDownListClass方法。

最後,DropDownListClass可能有一個更好的名稱,如Topic。

+0

不會拋出和dup鍵輸入異常嗎? –

+0

如何將DataTextField和DataTextValue分配給dropdownlsit? –

+1

@ Cubicle.Jockey不,它只會用新值替換舊值,如果已經存在的話。 –

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

你會如何執行? –

2

有幾個方法可以做到這一點。

一種方法是重寫DropDownListClassEquals方法。見Guidelines for Overriding Equals and Operator ==

另一個辦法是創建一個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); 

這告訴HashSet代碼,「用我的定義比較函數來比較項目「。你的第一個循環現在應該工作。

+0

下半代碼在哪裏編碼? insdie foreach循環? 'HashSet hashSetTopics = new HashSet (comparer); 「我不知道它會如何工作,除非我失去了一些東西 –