Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何查找我的数据是否在项目列表中_Vb.net_Search_Foreach_Contains_Loops - Fatal编程技术网

Vb.net 如何查找我的数据是否在项目列表中

Vb.net 如何查找我的数据是否在项目列表中,vb.net,search,foreach,contains,loops,Vb.net,Search,Foreach,Contains,Loops,在vb.net中 我有myList作为列表(myClass)和myData作为myClass 那么如何确定myList是否包含myData 目前我的做法是: dim myList as List(of myClass) = myClasses.GetData() dim myData as myClass = myClasses.getData(1) for each Item as myClass in myList if Item.uin = myData.uin then

在vb.net中
我有
myList作为列表(myClass)
myData作为myClass

那么如何确定myList是否包含myData

目前我的做法是:

dim myList as List(of myClass) = myClasses.GetData()
dim myData as myClass = myClasses.getData(1)

for each Item as myClass in myList
   if Item.uin = myData.uin then
       msgbox 'yeah'
   end if
next

下一个更好的解决方案是什么?

列表应该有一个Contains()方法来满足您的需要

如果需要偏移量,请使用indexOf()

编辑:如果由于某种原因,您的列表不包含相同的对象,并且您必须比较它们的“uin”字段是否相等,最简单的方法是使用LINQ:

myList.Any(Function(item) item.uin = myData.uin)
否则,泛型列表有一个Exists()方法,该方法接受一个谓词:

myList.Exists(Function(item) item.uin = myData.uin)

很抱歉,我的VB.NET不是很流利(我自己就是C#person),所以如果我在那里输入了任何错误,请告诉我。

我尝试过使用contains,但它对我来说不正常。。。即使它包含该元素,也返回false。我可以参考一些例子吗“uin”不是唯一的吗?Contains/indexOf将仅测试对象是否在列表中,而不测试该对象是否是列表中具有“uin”属性的对象是否等于特定值。因此,GetData()返回一个列表,其中包含与使用GetData(x)获得的对象不同的对象。奇怪。