Vb.net 正确使用List.Exists和谓词

Vb.net 正确使用List.Exists和谓词,vb.net,Vb.net,全部 我目前正在尝试按照 dim l_stuff as List(of Stuff) dim m_stuff as new Stuff m_stuff.property1 = 1 m_stuff.property2 = "This" if not l_stuff.exists(m_stuff) then l_stuff.add(m_stuff) end if 这显然失败了,因为Exist方法正在寻找Stuff的谓词 有人能完整地解释这个谓词,以及我如何实现我在这里要做的事情吗

全部

我目前正在尝试按照

dim l_stuff as List(of Stuff)

dim m_stuff as new Stuff

m_stuff.property1 = 1
m_stuff.property2 = "This"

if not l_stuff.exists(m_stuff) then
     l_stuff.add(m_stuff)
end if
这显然失败了,因为Exist方法正在寻找Stuff的谓词

有人能完整地解释这个谓词,以及我如何实现我在这里要做的事情吗

我试着用

if not l_stuff.contains(m_stuff) then
   l_stuff.add(m_stuff)
end if 
但是,这不会检测到idenitcal条目,而是在列表中输入一个副本

感谢(T的)列表。包含是您应该使用的方法。正如您所说,Exists需要一个谓词。当然,要使.Contains按预期工作,您需要重写
Equals()
方法以及
GetHashCode()

List(Of T).Exists
需要一个函数,当传递一个类型为T的项时,该函数将返回一个布尔值,在您的例子中,T的类型为Stuff。因此,您可以编写一个如下所示的方法:

If Not l_stuff.Exists(Function(x) x.property1 = m_stuff.property1 And _
x.property2 = m_stuff.property2) Then
等等