473,785 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# - Collection.Dist inct() and Generics question...

2 New Member
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 IEqualityCompar er<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 5793

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

Similar topics

7
2720
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 is the base for a collection and there are words in it and I want to create a new collection of sentenses where there is no similar secound word in those sentenses. How do I do this distinct selection from a collection of object? Thanks!
6
4841
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 and VB 8 Beta 1? Also, is there an easy way to force "v" to be an early bound variable. In VB 6 this can be
3
1353
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 x). If I make a private list as list(of x) in a class and implements IList(of x) I can control what methods to be public. I works - but now I can't bind the collection to a listbox og grid (it says something about complex datatype).
3
1187
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 dispose is called on all members of the collection. Calling Invoke on the collection calls Invoke on each member of the collection. http://www.geocities.com/jeff_louie/oop29.htm Regards, Jeff
4
3461
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 generics, but I realized that DataSets may also work here. I do need to display and edit lists of the items at various places. In the previous version I used a custom ListView for most display purposes. Given that DataSets in VS2005 have so many...
4
1761
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 I cannot find more detailed documentation for this behavior. What I mean is the invocation of the Dinosaurs.Add method which is derived from Collection(of String). The method seems to invoke the overridden InsertItem method. Can this method be...
14
1710
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 object. For each business object, I would have something like below: public class Apple { //apple stuff
7
5523
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, would return the value of obj because it returns the index of the item at position obj. Thoughts I had were to let the default method perform the default behavior and then create an overload as IndexOf(int index, ProductSearchCriteria criteria)...
7
5749
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 iterate through the collection and use "foreach" on the collection. public class Cars : System.Collections.ObjectModel.Collection<Car{ } So, could someone explain to me why you would want to create a GENERIC custom collection class.
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.