This is my class code that implements IEqualityComparer<T>:
Expand|Select|Wrap|Line Numbers
- public class Artist : IEqualityComparer<Artist>
- {
- public Artist()
- {
- }
- public Artist(string name)
- {
- this.Name = name;
- }
- private string _name;
- public string Name
- {
- get { return _name; }
- set { _name = value; }
- }
- #region IEqualityComparer Members
- public CaseInsensitiveComparer comparer = CaseInsensitiveComparer.Default;
- public bool Equals(Artist x, Artist y)
- {
- if (comparer.Compare(x.Name, y.Name) == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public int GetHashCode(Artist obj)
- {
- return obj.ToString().ToLower().GetHashCode();
- }
- #endregion
- }
Expand|Select|Wrap|Line Numbers
- public static IEnumerable<T> CreateTypeCollection<T>(AudioFileCollection files, Func<AudioFile, T> mapFunc, Func<T, string> orderFunc) where T : IEqualityComparer<T>
- {
- Collection<T> collection = new Collection<T>();
- foreach (AudioFile file in files)
- {
- collection.Add(mapFunc(file));
- }
- return collection.Distinct(EqualityComparer<T>.Default).OrderBy(orderFunc);
- }
Expand|Select|Wrap|Line Numbers
- _collection = LibraryBuilder.CreateTypeCollection<Artist>(AudioFileCollection, file => new Artist(file.Artist.ToLower()), artist => artist.Name);
Expand|Select|Wrap|Line Numbers
- collection.Distinct(EqualityComparer<T>.Default).
:)