Use of Distinct with list of Custom Object

Posted by Burnzy on Stack Overflow See other posts from Stack Overflow or by Burnzy
Published on 2010-06-09T14:23:19Z Indexed on 2010/06/09 14:42 UTC
Read the original article Hit count: 613

Filed under:
|
|
|

How can I make the Distinct() method work with a list of custom object(Href in this case), here is what the current object looks like:

    public class Href : IComparable, IComparer<Href>
    {
            public Uri URL { get; set; }
            public UrlType URLType { get; set; } 

           public Href(Uri url, UrlType urltype)
           {
                   URL = url;
                   URLType = urltype;
           }


           #region IComparable Members

           public int CompareTo( object obj )
           {
                   if(obj is Href)
                   {
                           return URL.ToString().CompareTo( ( obj as Href ).URL.ToString() );
                   } 
                   else
                           throw new ArgumentException("Wrong data type.");
           }

           #endregion

           #region IComparer<Href> Members

           int IComparer<Href>.Compare( Href x , Href y )
           {
                   return string.Compare( x.URL.ToString() , y.URL.ToString() );
           }

           #endregion
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ