Nick LaMarca
Nick LaMarca

Reputation: 8188

Finding Duplicates based On A Property In Object

I want to check for duplicates based on a field in an object.

I have an object called Item that has 3 properties

ID
Rank
Name

I have a list of type Item in a container called

lstTheItems

I am using this code to check for duplicates

'lstTheItems IS NOT CORRECT
 Dim duplicates = lstTheItems.GroupBy(Function(i) i) _
                                .Where(Function(g) g.Count() > 1) _
                                .[Select](Function(g) g.Key)

How do I return duplicate items base on the Name property?

Upvotes: 1

Views: 488

Answers (3)

Oscar Bralo
Oscar Bralo

Reputation: 1907

Dim duplicates = svgGrpContainer.Select(Function(x) svgGrpContainer.Count(Function(y)) > 1));

This means that we will select all the elements which appear more than once in the svgGrpContainer.

Function(x) = one element of svgGrpContainer

svgGrpContainer.Count = go through all the elements getting the count of..

Function(y) > 1 = means that we will take all the element which appear more than once

I hope this helps

Upvotes: 1

just.another.programmer
just.another.programmer

Reputation: 8785

You need to group it by the Name, no the object itself. Also, you probably want to get the entire object back in your duplicate list, not just the key (in this case the name).

Dim duplicates = lstTheItems.GroupBy(Function(i) i.Name) _
                            .Where(Function(x) x.Count() > 1) _
                            .[Select](Function(x) x)

Upvotes: 0

Nick LaMarca
Nick LaMarca

Reputation: 8188

Dim duplicates = svgGrpContainer.GroupBy(Function(x) x.Name).Where(Function(x) x.Count > 1).Select(Function(x) x)

Upvotes: 0

Related Questions