Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find duplicates in List(Of String) in VB.NET

Tags:

.net

list

vb.net

I have a customers List(of String) on which I am trying to find the duplicate customers.

If Not customers.Count = customers.Distinct.ToList.Count Then
     customers = customers.Except(customers.Distinct.ToList)
End If

But I get the following exception:

InvalidCastException
Unable to cast object of type '<ExceptIterator>d__99`1[System.String]' to type

'System.Collections.Generic.List`1[System.String]'.

Is this the right way to find duplicates in a list?

like image 837
Lenin Raj Rajasekaran Avatar asked Dec 04 '22 03:12

Lenin Raj Rajasekaran


1 Answers

customers = customers.GroupBy(Function(m) m) _
                 .Where(Function(g) g.Count() > 1) _
                 .Select(Function(g) g.Key).ToList
like image 134
Raphaël Althaus Avatar answered Jan 02 '23 17:01

Raphaël Althaus