我相信有人已经回答了这个问题,但是我很难找到正确的搜索词来定位它…
我当然可以通过循环所有的值来做到这一点,但我只是想看看是否有人知道一个更简单的方法。
Dim List1 As New List(Of Integer) From {1,3,5,7}
Dim List2 As New List(Of Integer) From {2,4,6,8}
List1.ContainsAnythingFrom(List2) = False
,因为在两个列表中都没有匹配的数字。
Dim List1 As New List(Of Integer) From {1,**3**,5,7}
Dim List2 As New List(Of Integer) From {2,**3**,6,8}
List1.ContainsAnythingFrom(List2) = True
因为每个列表中都有一个3
我正在寻找ContainsAnythingFrom类型函数。
您可以使用Enumerable。交叉LINQ方法查找常用项
Dim list1 = New Integer() {1, 2, 3, 4, 5}
Dim list2 = New Integer() {3, 4, 5, 6}
Dim list3 = New Integer() {7, 8}
Dim list1HasAnyOfList2 = list1.Intersect(list2).Any()
' true
Dim list1HasAnyOfList3 = list1.Intersect(list3).Any()
' false
Dim l1 As New List(Of String) From {"a", "b", "c", "d"}
Dim l2 As New List(Of String) From {"e", "f", "c", "d"}
Dim intersection As IEnumerable(Of String) = l1.Intersect(l2)
' Dim result As List(Of String) = l1.Intersect(l2).ToList()
For Each s In intersection
Console.WriteLine(s)
Next