在VB.NET中查找List(Of String)中的重复项

Len*_*ran 3 .net vb.net list

我有一个customersList(of String),我试图找到重复的客户.

If Not customers.Count = customers.Distinct.ToList.Count Then
     customers = customers.Except(customers.Distinct.ToList)
End If
Run Code Online (Sandbox Code Playgroud)

但我得到以下异常:

InvalidCastException
Unable to cast object of type '<ExceptIterator>d__99`1[System.String]' to type
Run Code Online (Sandbox Code Playgroud)

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

这是在列表中查找重复项的正确方法吗?

Rap*_*aus 8

customers = customers.GroupBy(Function(m) m) _
                 .Where(Function(g) g.Count() > 1) _
                 .Select(Function(g) g.Key).ToList
Run Code Online (Sandbox Code Playgroud)


ili*_*ian 8

VB版本:

Dim duplicates = listOfItems.GroupBy(Function(i) i)_
                            .Where(Function(g) g.Count() > 1)_
                            .[Select](Function(g) g.Key)
Run Code Online (Sandbox Code Playgroud)

C#:

var duplicates = customers.GroupBy(x => x)
                          .Where(g => g.Count() > 1)
                          .Select(g => g.Key);
Run Code Online (Sandbox Code Playgroud)