从列表中删除重复项

时间:2017-05-07 16:45:15

标签: vb.net

我有以下课程:

Public Class titlesclass
Public Property Link As String
Public Property Title As String

Public Function Clear()
    Link.Distinct().ToArray()
    Title.Distinct().ToArray()

End Function
End Class

以下代码:

For Each title As Match In (New Regex(pattern).Matches(content)) 'Since you are only pulling a few strings, I thought a regex would be better.
            Dim letitre As New titlesclass
            letitre.Link = title.Groups("Data").Value
            letitre.Title = title.Groups("Dataa").Value
            lestitres.Add(letitre)
            'tempTitles2.Add(title.Groups("Dataa").Value)
        Next

我尝试使用简单的方法删除重复的字符串

Dim titles2 = lestitres.Distinct().ToArray()

调用类函数:

lestitres.Clear()

但这两个命题都不起作用,我知道我错过了一些非常简单的东西,但仍然找不到它是什么

1 个答案:

答案 0 :(得分:2)

更容易使用已经实现IComparable的类:

Dim query = From title In Regex.Matches(content, pattern).Cast(Of Match)
            Select Tuple.Create(title.Groups("Data").Value, title.Groups("Dataa").Value)

For Each letitre In query.Distinct
    Debug.Print(letitre.Item1 & ", " & letitre.Item2)
Next

Anonymous Types

Dim query = From title In Regex.Matches(content, pattern).Cast(Of Match)
            Select New With {Key .Link = title.Groups("Data").Value, 
                             Key .Title = title.Groups("Dataa").Value}

For Each letitre In query.Distinct
    Debug.Print(letitre.Link & ", " & letitre.Title)
Next