4

customers重複した顧客を見つけようとしているList(of String) があります。

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]'.

これは、リスト内の重複を見つける正しい方法ですか?

4

2 に答える 2

9
customers = customers.GroupBy(Function(m) m) _
                 .Where(Function(g) g.Count() > 1) _
                 .Select(Function(g) g.Key).ToList
于 2012-06-27T14:45:59.187 に答える
8

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);
于 2012-06-27T14:59:55.180 に答える