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
(列の)それは新しいリストとして暗いremovedDupsなるはずの= oList.Distinct()(ToListメソッド – codeMonger123
'個別) 'は列挙子を返します。 'Debug.Print("シリンダー総数= "&oList.Distinct()。Count())'を実行することができます。おそらく 'List'の代わりに 'HashSet 'を使う方が妥当でしょう。 –
'newList = oList.Distinct()。ToList()' Distinctは新しいリストを返すメソッドですが、何かのように見えるオブジェクトの名前やテキストだけでなく、デフォルトの等価比較子を使って動作しますあなたはそこにいます – Plutonix