在VB.NET的List(Of String)中查找重复项 [英] Find duplicates in List(Of String) in VB.NET

查看:560
本文介绍了在VB.NET的List(Of String)中查找重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个customers字符串列表,我试图在该列表上查找重复的客户.

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

但是我得到以下异常:

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

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

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

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

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

推荐答案

customers = customers.GroupBy(Function(m) m) _
                 .Where(Function(g) g.Count() > 1) _
                 .Select(Function(g) g.Key).ToList

这篇关于在VB.NET的List(Of String)中查找重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆