从List(Of T)中删除重复项 [英] Remove duplicates from List(Of T)

查看:112
本文介绍了从List(Of T)中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除List(Of String)中的重复项?我当时以为它可以与List(Of T).Distinct一起使用,但是我的结果却相反.我究竟做错了什么?还是我要更改什么才能删除List(Of T)中的重复项?

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)?

我已经在全球网络上阅读了一些有关对哈希进行哈希处理的内容,但我认为这并不是必须的.

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

这是我生成列表的代码(与Autodesk Inventor一起使用).

This is my code where the list is generated (works with 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

推荐答案

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

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

ListOfString.Distinct().ToList

这篇关于从List(Of T)中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆