从 List(Of T) 中删除重复项

4
如何在 List(Of String) 中删除重复项?我原以为可以使用 List(Of T).Distinct,但结果并非如此。我做错了什么?还是说我需要改变什么才能删除 List(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
不应该是这样吗?Dim removedDups As New List(Of String) = oList.Distinct().ToList。 - codeMonger123
Distinct() 返回一个枚举器。您可以使用 Debug.Print("Total number of cylinders = " & oList.Distinct().Count())。使用 HashSet<T> 而不是 List<T> 可能更合理。 - Nico Schertler
7
newList = oList.Distinct().ToList() 这里的 Distinct 方法会返回一个新的列表,但它使用默认的相等比较器进行操作,而不仅仅是对象中的名称或某些文本,这似乎是你在那里做的事情。 - Ňɏssa Pøngjǣrdenlarp
4
Enumerable.Distinct是一个LINQ扩展方法,它返回一个没有重复项的序列(如果类型覆盖了类似字符串的GetHashCodeEquals)。 如果您想从列表中删除重复项,则必须将其重新分配给列表变量:oList = oList.Distinct().ToList()ToList创建一个带有唯一字符串的新列表。 - Tim Schmelter
3个回答

10

2
Imports System.Linq

...

Dim oList As New List(Of String)
oList.Add("My Cylinder")
oList = oList.Distinct.ToList()

需要包含 System.Linq


1
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

1
我不确定为什么你的回答遭到了负评,因为它是有效的。可能是因为评论中已经有了一个答案(oList = oList.Distinct().ToList())。你可以通过在Exist = True之后使用Exit For来使你的代码更高效,因为一旦你知道元素在结果列表中,就不需要再检查Result的任何其他元素了。 - Andrew Morton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接