12

I have a list of type MyClass

public class MyClass
{
   public string prop1 {} 
   public int prop2 {} 
   public string prop3 {} 
   public int prop4 {} 
   public string prop5 {} 
   public string prop6 {} 
   ....
}

This list will have duplicates. I want to find and remove items from this list where prop1, prop2 and prop3 are duplicates. It doesnt matter if the other properties are duplicates

This is what I have tried that is not working.

List<MyClass> noDups = myClassList.GroupBy(d => new {d.prop1,d.prop2,d.prop3} ).Where(g => g.Count() > 1).Select(g=> g.Key);

I dont want to use any third party tools for this. Only pure linq.

leppie
  • 115,091
  • 17
  • 196
  • 297
user20358
  • 14,182
  • 36
  • 114
  • 186
  • 1
    What do you mean by _not working_? You get any exception or error message? What do you get as a result? – Soner Gönül Feb 25 '15 at 09:49
  • I get an exception Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?) – user20358 Feb 25 '15 at 09:53
  • when put a ToList at the end of the Select(g=> g.Key)... I get "Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List' – user20358 Feb 25 '15 at 09:55
  • when I return to a var type and do a to list on that I only get the fields I have put in within the { } brackets. I want the remaining fields too.. – user20358 Feb 25 '15 at 09:57

1 Answers1

32

This will return one item for each "type" (like a Distinct) (so if you have A, A, B, C it will return A, B, C)

List<MyClass> noDups = myClassList.GroupBy(d => new {d.prop1,d.prop2,d.prop3} )
                                  .Select(d => d.First())
                                  .ToList();

If you want only the elements that don't have a duplicate (so if you have A, A, B, C it will return B, C):

List<MyClass> noDups = myClassList.GroupBy(d => new {d.prop1,d.prop2,d.prop3} )
                                  .Where(d => d.Count() == 1)
                                  .Select(d => d.First())
                                  .ToList();
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • what if you need to do the same thing with a `datatable` instead of `MyClass`? – Jogi May 10 '16 at 09:19
  • @RehanKhan Totally different question... Ask a new question about it, easier. – xanatos May 10 '16 at 09:20
  • if you are interested, [here](http://stackoverflow.com/questions/37154701/distinct-on-multiple-columns-in-datatable?noredirect=1#comment61846505_37154701) is that question. – Jogi May 11 '16 at 07:12