问候语。我目前有点在Object (int,string)
我想检查字符串是否已经包含在列表中 - 如果没有添加它。
常规<List>.Contains()
无法正常工作,因为我在插入新对象之前正在初始化它。
这是我的代码:
Dim command = New SqlCommand(query, connection)
Dim reader = command.ExecuteReader()
While reader.Read
Dim projID = reader.Item("DB_ID")
Dim projName = reader.Item("Project")
Dim proj = New KMProject(projName, projID)
projList.Add(proj)
End While
reader.Close()
=============================== ProjList having some items already =====================================
query = ...
command = New SqlCommand(query, connection)
reader = command.ExecuteReader()
While reader.Read
Dim projID = reader.Item("DB_ID")
Dim projName = reader.Item("Project")
Dim proj = New KMProject(projName, projID)
===============Below this section im stuck while verifying if this String is already in the List ===================================
For Each p In projList
If projList.Contains() Then
Else
projList.Add(proj)
End If
Next
答案 0 :(得分:1)
在我看来,你最好的选择是LINQ:
If Not progList.Any(Function(x) x.Name = "Your string to check") Then
/*Add the thing*/
End If
不要忘记你必须添加Imports声明:
Imports System.Linq;
答案 1 :(得分:0)
你可以使用Linq ......
For Each p in projlist
If Not projlist.Any(Function(pl) (pl.ID = proj.ID)) Then prodlist.Add(proj)
Next
(将ID替换为您对象的属性名称)