linq - C# 3.0: Need to return duplicates from a List<> -


I have a list of objects in C # and I need to return the object that is considered to be a duplicate in the list . I do not need specific results, I need a list of items that I am removing from my repository.

For this example, say I have a list of "cars" type and I need to know which of these cars are similar to the second color in the list. Here is the property of cars and their color in the list:

  Car1.Color = Red; Car2.Color = Blue; Car3.Color = Green; Car4.Color = Red; Car5.Color = Red;  

For this example, I need the results (IEnumerable ;,> >, or whatever) to include Car4 and Car5, because I have them in my repository DB to remove so that I only have one car per color in my repository Any help would be appreciated.

I was unconsciously coded yesterday, when I was trying to write a "specific" by launch One included! When I should not be, but this time it is right:

  Public stable IEnumerable & lt; TSource & gt; Duplicate By & lt; TSource, TKey & gt; (This IEnumerable & lt; TSource & gt; source, function & lt; TSource, TKey & gt; key selector) {HashSet & lt; TKey & gt; Scenes = NewHashet & lt; TKey & gt; (); Foreign object (source in source) {// to generate it if the key is not actually included - that is // already in the set if (see KA.Ed. (key selector)) {Yield returns element; }}}  

Then you will call it at:

  var duplicates = cars Duplicates (car => car color);  

Comments