在VB.NET的列表(字符串)中查找重复项
我有一个在VB.NET的列表(字符串)中查找重复项,.net,vb.net,list,.net,Vb.net,List,我有一个客户列表(字符串),我正试图在其中查找重复的客户 If Not customers.Count = customers.Distinct.ToList.Count Then customers = customers.Except(customers.Distinct.ToList) End If 但我有以下例外: InvalidCastException 无法将类型为“d_u99`1[System.String]”的对象强制转换为 'System.Collections.
客户
列表(字符串),我正试图在其中查找重复的客户
If Not customers.Count = customers.Distinct.ToList.Count Then
customers = customers.Except(customers.Distinct.ToList)
End If
但我有以下例外:
InvalidCastException
无法将类型为“d_u99`1[System.String]”的对象强制转换为
'System.Collections.Generic.List'1[System.String]'
这是在列表中查找重复项的正确方法吗?VB版本:
customers = customers.GroupBy(Function(m) m) _
.Where(Function(g) g.Count() > 1) _
.Select(Function(g) g.Key).ToList
Dim duplicates = listOfItems.GroupBy(Function(i) i)_
.Where(Function(g) g.Count() > 1)_
.[Select](Function(g) g.Key)
C#:
谢谢,但似乎没有删除重复项。此步骤后的客户计数为0。(customers.count=581,customers.Distinct.ToList.count=575)@emaillenin是的,因为这正是我们试图做的(这确实不是那么聪明)。看看我的编辑,也许?我想找到重复的。。所以减去原始列表和不同的列表将得到。。我不知道如何减去它们。。
Dim duplicates = listOfItems.GroupBy(Function(i) i)_
.Where(Function(g) g.Count() > 1)_
.[Select](Function(g) g.Key)
var duplicates = customers.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(g => g.Key);