繁体   English   中英

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

[英]Find duplicates in List(Of String) in VB.NET

我有一个customers列表(字符串),我试图在其中找到重复的客户。

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

但我得到以下异常:

 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

VB版本:

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

C#:

var duplicates = customers.GroupBy(x => x)
                          .Where(g => g.Count() > 1)
                          .Select(g => g.Key);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM