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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37349688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:53:53  来源:igfitidea点击:

Remove duplicates from List(Of T)

vb.netlistduplicates

提问by Mech_Engineer

How can I remove my duplicates in the List(Of String)? I was under the assumption that it could work with List(Of T).Distinct, but my result says otherwise. What am I doing wrong? Or what do I have to change to remove the duplicate items in the List(Of T)?

如何删除我的重复项List(Of String)?我假设它可以与 一起使用List(Of T).Distinct,但我的结果并非如此。我究竟做错了什么?或者我必须更改什么才能删除List(Of T).

I have read something on the worldwide web about hash something, but I don't think that is really necessary.

我在万维网上读过一些关于哈希的东西,但我认为这不是真的必要。

This is my code where the list is generated (works with Autodesk Inventor).

这是我生成列表的代码(适用于 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

Here is my debug output from the code:

这是我的代码调试输出:

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

回答by Mark

This is the answer you're looking for thanks to dotnetperls.com VB.NET Remove Duplicates From List

由于 dotnetperls.com VB.NET 从列表中删除重复项,这就是您正在寻找的答案

ListOfString.Distinct().ToList

回答by Johann Camboly

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

回答by Zeus Paez

Imports System.Linq

导入 System.Linq

Dim YOURList As New List(Of String)

将您的列表调暗为新列表(字符串)

YOURList.Add(item.Text)

YOURList.Add(item.Text)

YOURList = YOURList.Distinct.ToList

YOURList = YOURList.Distinct.ToList

It's necessary to include System.Linq

有必要包含 System.Linq