我有一个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)
但我得到以下异常:
Run Code Online (Sandbox Code Playgroud)InvalidCastException Unable to cast object of type '<ExceptIterator>d__99`1[System.String]' to type'System.Collections.Generic.List`1 [System.String]'.
这是在列表中查找重复项的正确方法吗?
customers = customers.GroupBy(Function(m) m) _
.Where(Function(g) g.Count() > 1) _
.Select(Function(g) g.Key).ToList
Run Code Online (Sandbox Code Playgroud)
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)