簡體   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