2012-06-27 2 views
1

중복 고객을 찾으려고 시도한 customers List (String 중) 목록이 있습니다.VB.NET의 List (Of String)에서 중복을 찾으십시오.

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

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

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

는하지만 다음과 같은 예외를 얻을.

목록에서 중복 된 것을 찾는 것이 올바른 방법입니까?

답변

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

감사합니다,하지만 것 같다 제외하고 중복을 제거하지. 고객 수는이 단계 후에 0입니다. (customers.count = 581, customers.Distinct.ToList.count = 575) – emaillenin

+0

@emillillenin 네, 정확히 우리가하려고하는 것입니다. (실제로 똑똑하지 않습니다.) 내 편집 좀 봐, 아마도? –

+0

나는 ** ** duplicates를 찾고 싶다. .. 그래서 원래의리스트와 구분 된리스트를 빼면 .. 그걸 빼는 방법을 모른다 .. – emaillenin

5

비주얼 베이직 버전 :

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); 
관련 문제