title: Remove duplicates from listbox/combobox in WPF
body: <p>I've seen answers mentioning something compact like this:<a href="https://stackoverflow.com/questions/47752/remove-duplicates-from-a-listt-in-c">here</a></p>
  
  <pre><code>List&lt;T&gt; withDupes = LoadSomeData();
  List&lt;T&gt; noDupes = withDupes.Distinct().ToList();
  </code></pre>
  
  <p>So I tried the following (syntax)</p>
  
  <pre><code>List&lt;InfoControl&gt; withDupes = (List&lt;InfoControl&gt;)listBox1.ItemsSource;
  listBox1.ItemsSource = withDupes.Distinct().ToList();
  </code></pre>
  
  <p>but withDupes is null ?  Perhaps I am retrieving the wrong data list. I added InfoControls one at a time.</p>
  
  <p>Is there something else I should be implementing in InfoControl class? (Equal,hashCode)?</p>
  
  <p>Thanks
  Addendum 1: [ignore I should not be translating from Java :) ]
  Also have (translated from a Java example, not sure it's 100% correct) declared in the InfoControl class..</p>
  
  <pre><code>public Boolean Equals(Object obj) 
  { if (obj == this) { return true; } 
  if (!(obj is InfoControl)) { return false; } 
  
  InfoControl other = (InfoControl)obj; 
  return this.URL.Equals(other.URL); } 
  
  public int hashCode() 
  { return this.URLFld.Content.GetHashCode(); } 
  </code></pre>
  
  <p>Addendum 2:
  When I try to use override based on the msdn link custom type example it says it is sealed :) 
  It does not seem distinct is stepping thru GetHashCode() and I am still getting the same listbox.items.count after distinct.</p>
  
  <pre><code>bool IEquatable&lt;InfoControl&gt;.Equals(InfoControl other)
  {
  if (Object.ReferenceEquals(other, null)) return false;
  if (Object.ReferenceEquals(this, other)) return true;
  return URL.Equals(other.URL);
  }
  
  public int GetHashCode(InfoControl obj)
  {
       return obj.URL.GetHashCode();
  }
  </code></pre>
  
  <p>Addendum 3:
  When I try override VS2010 says it is sealed? "cannot override inherited member 'System.Windows.DependencyObject.GetHashCode()' because it is sealed" what am I doing wrong?</p>
  
  <pre><code>  public override int GetHashCode()
      {
          return URL.GetHashCode();
      }
  
     public string URL
      {
          get { return this.URLFld.Content.ToString() ; }
          set
          {
              this.URLFld.Content = value;
          }
      }
  </code></pre>
  
  <p>.
  Addendum 4:</p>
  
  <pre><code>   public partial class InfoControl : UserControl
           , IEquatable&lt;YouTubeInfoControl&gt;
  
      {
  
          private string URL_;
          public string URL
          {
              get { return URL_; }
              set
              {
                  URL_ = value;
              }
          }
  
          bool IEquatable&lt;YouTubeInfoControl&gt;.Equals(YouTubeInfoControl other)
          {
  
              if (Object.ReferenceEquals(other, null)) return false;
  
  
              if (Object.ReferenceEquals(this, other)) return true;
  
              return URL == other.URL;
          }
  
          public override int GetHashCode()
          {
              return URL.GetHashCode();
          }
  
      }
  </code></pre>