2016-05-20 27 views
0

List(Of String)で重複を削除するにはどうすればよいですか?私はそれがList(Of T).Distinctと働くことができるという仮定の下にあったが、私の結果はそうではないと言います。私は間違って何をしていますか?または、List(Of T)の重複アイテムを削除するために何を変更する必要がありますか?リスト(Of T)から重複を削除

ハッシュについては世界中のウェブで何かを読んだことがありますが、それは本当に必要ではないと思います。

これはリストが生成される私のコードです(Autodesk Inventorで動作します)。ここで

Private Function CountCylinders(ByVal oDef As AssemblyComponentDefinition) As Integer 

    ' Lets list all cylinder segments found in the assembly 
    ' we will need the document name to do this. 
    ' the initial value is nothing, if, after counting 
    ' this is still the case, there are no cylinders. 
    Dim oList As New List(Of String) 

    ' Loop through all of the occurences found in the assembly 
    For Each oOccurrence As ComponentOccurrence In oDef.Occurrences 

     ' Get the occurence document 
     Dim oOccurenceDocument As Document 
     oOccurenceDocument = oOccurrence.Definition.Document 

     ' Check if the occurence document name contains cylinder 
     If oOccurenceDocument.FullFileName.Contains("Cylinder") Then 
      ' Get the cylinder filename 
      Dim oCylinder As String 
      oCylinder = oOccurenceDocument.FullFileName 

      ' Get the filename w/o extension 
      oCylinder = IO.Path.GetFileNameWithoutExtension(oCylinder) 

      ' Remove the segment mark. 
      oCylinder = oCylinder.Remove(oCylinder.LastIndexOf("_"), oCylinder.Length - oCylinder.LastIndexOf("_")) 

      oList.Add(oCylinder) 
      Debug.Print("add : " & oCylinder) 
     End If 
    Next 

    ' Delete the duplicates in the list 
    oList.Distinct() 

    ' TODO: can be removed. 
    Debug.Print("Total number of cylinders = " & oList.Count) 

    ' Return the number of cylinders 
    CountCylinders = oList.Count 

End Function 

はコードから、私のデバッグ出力です:。

add : Cylinder_1 
add : Cylinder_2 
add : Cylinder_2 
add : Cylinder_2 
add : Cylinder_2 
add : Cylinder_2 
add : Cylinder_7 
Total number of cylinders = 7 
+3

(列の)それは新しいリストとして暗いremovedDupsなるはずの= oList.Distinct()(ToListメソッド – codeMonger123

+0

'個別) 'は列挙子を返します。 'Debug.Print("シリンダー総数= "&oList.Distinct()。Count())'を実行することができます。おそらく 'List 'の代わりに 'HashSet 'を使う方が妥当でしょう。 –

+5

'newList = oList.Distinct()。ToList()' Distinctは新しいリストを返すメソッドですが、何かのように見えるオブジェクトの名前やテキストだけでなく、デフォルトの等価比較子を使って動作しますあなたはそこにいます – Plutonix

答えて

0
Function RemoveDuplicate(ByVal TheList As List(Of String)) As List(Of String) 
    Dim Result As New List(Of String) 

    Dim Exist As Boolean = False 
    For Each ElementString As String In TheList 
     Exist = False 
     For Each ElementStringInResult As String In Result 
      If ElementString = ElementStringInResult Then 
       Exist = True 
       Exit For 
      End If 
     Next 
     If Not Exist Then 
      Result.Add(ElementString) 
     End If 
    Next 

    Return Result 
End Function 
+0

私はあなたの答えがなぜdownvoteを持っているのか分かりません。おそらくコメントには既に回答があるからです( 'oList = oList.Distinct()。ToList()')。要素が結果リストにあるとすぐに 'Result'の要素をチェックする必要がないので、' Exist = True'の後の行に 'Exit For'を使用するとコードをより効率的にすることができます。 –

+0

はい、アンドリュー。私はこれを今すぐ修正する –

関連する問題