-2

How do we find duplicates in a list of objects based on multiple properties?

The following code is not returning if the list contains dupes or not.

var dups = ListItems.GroupBy(i => new { i.Value, i.Code })
                    .Where(g => g.Count() >= 1)
                    .Select(g => new 
                     { 
                        Length = g.Key.Value,
                        Label = g.Key.Code,
                        Count = g.Count()
                      });
Andrey Tretyak
  • 3,043
  • 2
  • 20
  • 33
Raghu Dongur
  • 11
  • 1
  • 1
  • 3
    Dear Raghu! Please, provide a tag for the programming language. Use code formatting (backticks or {} button in the editor panel) – John_West Jan 05 '16 at 20:39
  • I think this question has been asked before. Also the second sentence is not clear whether your code is not returning at all, or if you expect that it will tell you if there are duplicates in the list. – Mauricio Quintana Jan 05 '16 at 20:47

1 Answers1

2

I guess you have error in condition Where(g => g.Count() >= 1) it will return all groups with one or more items (actually all group will have at least one item so this condition is always true).

If you want to get only duplicates it should be: Where(g => g.Count() > 1).

So result code will be:

var dups = ListItems.GroupBy(i => new { i.Value, i.Code })
                    .Where(g => g.Count() > 1)
                    .Select(g => new 
                     { 
                        Length = g.Key.Value,
                        Label = g.Key.Code,
                        Count = g.Count()
                     });

In this way in dups you will have groups of more than one items with duplicated Value and Code.

Also I think it will be better if you will call Select before Where because now you are calculating count of items in group twice, inside Where to check condition and inside Select to store count.

If you will call Where after Select you will be able to filter using stored value of group count, like this:

var dups = ListItems.GroupBy(i => new { i.Value, i.Code })
                    .Select(g => new 
                     { 
                        Length = g.Key.Value,
                        Label = g.Key.Code,
                        Count = g.Count()
                     })
                    .Where(g => g.Count > 1);
Andrey Tretyak
  • 3,043
  • 2
  • 20
  • 33