在两个通用列表中使用方法 FindAll

Using method FindAll in two generic lists

我在 vb.net 程序中有两个通用列表。我想循环 List_A 并在 List_B 中搜索 List_A.ID,共同的元素应该存储在第三个列表 (LIST) 中。

For Each n As BE_Busq In List_A

    LIST = List_B.FindAll(Function(x As BE_Busq) x.ID = n.ID)
    '' for each step, LIST should be incremented, not be replaced
Next

FindAll 方法将 return 一个通用列表。如何递增 LIST 而不是为循环中的每个步骤替换它?

试试这个:

LIST.addrange(List_B.FindAll(Function(x As BE_Busq) x.ID = n.ID))

您可以使用 AddRange 方法将多个项目添加到列表中。

For Each n As BE_Busq In List_A
    LIST.AddRange(List_B.FindAll(Function(x As BE_Busq) x.ID = n.ID))
Next