问题描述
我有一个vb程序,我想使用列表一个值并检查来自2个列表的重复项。
我在两个列表中查找的值的变量名是 strCaseEventInternalID
列出一个 strCaseEventInternalIDList 有3个值1810458047,1810458073和1810458074.
列表2有3个值1810458073,1810458074和1810458074
列表2在此对象中
I have a vb program and I would like to use list one values and check for duplicates from 2 lists.
The variable name for the values I am looking for in both lists is strCaseEventInternalID
List one strCaseEventInternalIDList have 3 values 1810458047, 1810458073 and 1810458074.
List two have 3 values 1810458073, 1810458074 and 1810458074
List two is in this object
objXmlResponseDoc2 = Msc.Integration.Mncis.Library.v4.Odyssey.QueryDB(strSql, "Justice", False, True)
在列表2中有一个副本(1810458074),这是我想要找到的。
我该怎么做?
我尝试了什么:
There is a duplicate (1810458074) in list two which is what I want to find.
How do I do that?
What I have tried:
For Each strCaseEventInternalID In strCaseEventInternalIDList
If (strCaseEventInternalIDList.Count() > 1) Then
'Do something
End If
Next
推荐答案
Dim l1 = New List(Of Integer) From {1810458047, 1810458073, 1810458074}
Dim l2 = New List(Of Integer) From {1810458073, 1810458074, 1810458074}
'find item that exists in l1 but not l2
Dim l3 = l1.Except(l2).ToList() ' result is {1810458047}
'find item in both List
Dim l4 = l1.Intersect(l2).ToList() ' result is {1810458073,1810458074}
'find duplicate in a List
Dim duplicates1 = l1.Where(Function(x) l1.Where(Function(y) x = y).Count() > 1).Distinct() 'result is NOTHING
Dim duplicates2 = l2.Where(Function(x) l2.Where(Function(y) x = y).Count() > 1).Distinct() 'result is 1810458074
'find duplicates item in l2 that exists in l1
Dim l5 = l2.Intersect(l1).Where(Function(x) l2.Where(Function(y) x = y).Count() > 1).Distinct().ToList() 'result 1810458074
'find duplicates item in l1 that exists in l2
Dim l6 = l1.Intersect(l1).Where(Function(x) l1.Where(Function(y) x = y).Count() > 1).Distinct().ToList() 'result is NOTHING
[]
[ ^ ]
For Each val1 As Integer In list1
For Each val2 As Integer In list2
If val1 = val2 Then
' found duplicate
End If
Next
Next
(编辑:添加'然后'与'如果')
( added 'Then' with 'If')
列表二有3值1810458073,1810458074和1810458074
有一个重复(18104580) 74)在列表2中,这是我想要找到的。
List two have 3 values 1810458073, 1810458074 and 1810458074
There is a duplicate (1810458074) in list two which is what I want to find.
我理解为在单个列表中搜索重复项,因为列表包含1810458074 2次。
I understand this as searching duplicates in a single list as the list contains 1810458074 2 times.
Dim list1 = New List(Of Integer) From {1810458073, 1810458074, 1810458074}
For x = LBound(list1) to UBound(list1) - 1
For y = x + 1 to UBound(list1)
If list1(x) = list1(x) then
' found duplicate
End If
Next
Next
这篇关于如何从两个列表中获取重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!