473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C# - Collection.Distinct() and Generics question...

I have a generic method that performs some basic sorting and removal of duplicates on some classes, but I cannot get the parameter to pass into the Distinct function working.

This is my class code that implements IEqualityComparer<T>:

Expand|Select|Wrap|Line Numbers
  1.     public class Artist : IEqualityComparer<Artist>
  2.     {
  3.         public Artist()
  4.         {
  5.         }
  6.  
  7.         public Artist(string name)
  8.         {
  9.             this.Name = name;
  10.         }
  11.  
  12.         private string _name;
  13.         public string Name
  14.         {
  15.             get { return _name; }
  16.             set { _name = value; }
  17.         }
  18.  
  19.         #region IEqualityComparer Members
  20.  
  21.         public CaseInsensitiveComparer comparer = CaseInsensitiveComparer.Default;
  22.  
  23.         public bool Equals(Artist x, Artist y)
  24.         {
  25.             if (comparer.Compare(x.Name, y.Name) == 0)
  26.             {
  27.                 return true;
  28.             }
  29.             else
  30.             {
  31.                 return false;
  32.             }
  33.         }
  34.  
  35.         public int GetHashCode(Artist obj)
  36.         {
  37.             return obj.ToString().ToLower().GetHashCode();
  38.         }
  39.  
  40.         #endregion
  41.     }
this is the generic method that performs a sort and removal of duplicates of Artist (and other classes):

Expand|Select|Wrap|Line Numbers
  1.         public static IEnumerable<T> CreateTypeCollection<T>(AudioFileCollection files, Func<AudioFile, T> mapFunc, Func<T, string> orderFunc) where T : IEqualityComparer<T>
  2.         {
  3.             Collection<T> collection = new Collection<T>();
  4.             foreach (AudioFile file in files)
  5.             {
  6.                 collection.Add(mapFunc(file));
  7.             }
  8.             return collection.Distinct(EqualityComparer<T>.Default).OrderBy(orderFunc);
  9.         }
and this is the method call:

Expand|Select|Wrap|Line Numbers
  1.             _collection = LibraryBuilder.CreateTypeCollection<Artist>(AudioFileCollection, file => new Artist(file.Artist.ToLower()), artist => artist.Name);
everything works fine apart from this bit:

Expand|Select|Wrap|Line Numbers
  1. collection.Distinct(EqualityComparer<T>.Default).
i want to pass the 'Equals(Artist x, Artist y)' implementation into the Distinct() method but in a generic way. i cant get it working, any help is much appreciated.

:)
May 6 '08 #1
0 5775

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: juli | last post by:
I have strings variables in a collection list and I want to create new collection but to add to it only strings that are distinct (no common strings). For example I have an object sentense which...
6
by: Michael D. Ober | last post by:
In VB 6, the loop iterator v in the following code must be a variant. dim v as variant dim c as new collection for each v in collection ... next v What is the general translation in VB 7.1...
3
by: Michael Fällgreen | last post by:
Hi, I need to make a strongly typed collection of x, and the simplets way is to inherits list(of x). With that I can bind to a list-control. However - i don't need all the methods from list(of...
3
by: Jeff Louie | last post by:
Here is my try at generics. This creates a collection that implements IInvoke and IDisposable and Adds types that implement IInvoke and IDisposable. When the collection goes out of using scope...
4
by: _DS | last post by:
I need to create arrays of a custom data type (which is composed of various strings, ints, the usual). The original was built using CollectionBase. I was thinking about redesigning to use...
4
by: Robert Schneider | last post by:
Hi, the msdn documentation provides an Dinosaurs example that uses the objectmodel.collection class. Just have a look on the help page for this class. I don't understand what happens here. Or...
14
by: cwineman | last post by:
Hello, I'm hoping to do something using Generics, but I'm not sure it's possible. Let's say I want to have a bunch of business objects and a data access class cooresponding to each business...
7
by: Dale | last post by:
I have a design question. I am creating a custom collection of products. The unique key for the products is productId which is an integer. By default, IndexOf(object obj), when obj is an int,...
7
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
Can someone explain a few things about collections to me. In C# 2 generics, you can create a collection class by inheriting from System.Collections.ObjectModel.Collection. Using this you can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.