你可以使用
原文链接:https://www.f2er.com/vb/255786.htmlList.Contains
:
If Not lsAuthors.Contains(newAuthor) Then lsAuthors.Add(newAuthor) End If
或者使用LINQs Enumerable.Any:
Dim authors = From author In lsAuthors Where author = newAuthor If Not authors.Any() Then lsAuthors.Add(newAuthor) End If
您还可以使用高效的HashSet(Of String)
而不是不允许重复的列表,如果字符串已经在集合中,则在HashSet.Add
中返回False.
Dim isNew As Boolean = lsAuthors.Add(newAuthor) ' presuming lsAuthors is a HashSet(Of String)