如何从两个列表中获取重复值? [英] How do I get duplicate values from two lists?

查看:96
本文介绍了如何从两个列表中获取重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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

推荐答案

不清楚你的问题是否在询问在列表中或两个列表之间查找副本。无论如何,这里有关于如何完成这两个场景的示例

Not clear if your question is asking to find duplicate in a list or in between two list. Anyway, here are examples on how to accomplish both scenario
 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



.net - 如何从vb.net中的列表中获取重复项 - Stack Overflow [ ^ ]



vb.net - 比较两个列表并获得差异 - Stack Overflow [ ^ ]


如果你想使用'For Each'(并不关心list1或list2本身是否有重复):

If you want to use 'For Each' (and don't care if there are duplicates in list1 or list2 itself):
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


这篇关于如何从两个列表中获取重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆