Vb.net 循环遍历不同类型对象的组合列表

Vb.net 循环遍历不同类型对象的组合列表,vb.net,Vb.net,我有两张单子。一个是Foo类型,另一个是Bar类型 Foo和Bar碰巧都具有a的属性 是否有一种方法可以循环检查这两个列表,并将它们相互结合,以检查属性a的值 伪代码 for each item in FooList + BarList if item.a = "this is the value" then return True end if next 我不想单独循环浏览这些列表,尽管我知道这会起作用 我不希望原始列表由于追加而被修改 Linq的回答是可以接

我有两张单子。一个是Foo类型,另一个是Bar类型

Foo和Bar碰巧都具有a的属性

是否有一种方法可以循环检查这两个列表,并将它们相互结合,以检查属性a的值

伪代码

for each item in FooList + BarList
    if item.a = "this is the value" then
        return True
    end if
next
我不想单独循环浏览这些列表,尽管我知道这会起作用

我不希望原始列表由于追加而被修改

Linq的回答是可以接受的

上下文是我正在搜索DXF文件中的所有文本。其中一些文本被称为MTEXT,而另一些则被简单地称为文本。多行文字只是一种具有更多功能的文字,但无论其他属性是什么,这两种内容显然都包含有共同的价值属性

使现代化 根据公认答案中的代码,我能够想出一个快速内联方法

for each item in new List(Of Object)().Concat(FooList).Concat(BarList)
    if item.a = "this is the value" then
        return True
    end if
next

我相信这会引起很多人的愤怒,但这里有一个方法可以做到这一点。将列表强制转换为对象,然后将它们合并在一起,以便可以对其进行枚举。现在使用CallByName从他们那里获取A属性:

Public Class Form1

    Private FooList As New List(Of Foo)
    Private BarList As New List(Of Bar)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FooList.Add(New Foo() With {.A = "Bob"})
        FooList.Add(New Foo() With {.A = "Dora"})
        BarList.Add(New Bar() With {.A = "Pikachu"})
        BarList.Add(New Bar() With {.A = "Aang"})
        BarList.Add(New Bar() With {.A = "Joe"})
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each O As Object In FooList.Cast(Of Object).Concat(BarList.Cast(Of Object))
            Dim A As String = CallByName(O, "A", CallType.Get)
            Debug.Print(A)
        Next
    End Sub

End Class

Public Class Foo

    Public Property A As String

End Class

Public Class Bar

    Public Property A As String

End Class

FWIW,这里是一个使用匿名类型和LINQ的解决方案

Private foos As New List(Of Foo)
Private bars As New List(Of Bar)

Sub Main()
    foos.Add(New Foo() With {.A = "this is not the value"})
    foos.Add(New Foo() With {.A = "this is not the value"})
    bars.Add(New Bar() With {.A = "this is the value"})
    bars.Add(New Bar() With {.A = "this is not the value"})
    Console.WriteLine($"Found the value: {fooBar()}. Press any key to exit!")
    Console.ReadLine()
End Sub

Private Function fooBar() As Boolean
    Dim combinedList = foos.Select(Function(f) New With {f.A}).Concat(bars.Select(Function(f) New With {f.A}))
    For Each item In combinedList
        If item.A = "this is the value" Then Return True
    Next
    Return False
End Function

Public Class Foo
    Public Property A As String
End Class

Public Class Bar
    Public Property A As String
End Class

创建一个具有属性a的接口,并在两个类中实现它。然后,您可以创建一个接口列表并迭代其项,而不是创建两个列表。@AhmedAbdelhameed类不能修改。我担心你会这么说。它们有共同点吗?您可以在直接强制转换它们时将它们作为分母进行迭代,以查看if-so的内容;没什么大不了的!如果需要,可以将两个列表合并到一个ListOf对象中,迭代其项并使用TryCast访问a属性,但这根本不是一个好主意。只需使用两个循环。@djv True。你说得对。哈哈。我只是在为图书馆作家辩护。另外,这更多地来自于脚本的角度,在脚本中,您不局限于类型,因此您可以拥有一个包含任意数量类型的数组,并在其中循环查找特定属性。谢谢,虽然我认为这很有效,但无论如何,我已经开始得出结论了。这是一件令人困惑的事情,这是可以理解的,但在我的具体情况下,这是我做这件事的理想方式。@bigb556677从你对问题的编辑判断,这一段是上下文…,这似乎是一个错误。可能有更好的解决方案,但您似乎使用了错误的示例来演示您实际要做的事情。另外,这可能也是你的问题获得否决票的部分原因。@AhmedAbdelhameed承认我的问题是如何将两个不同类型的列表合并起来,这正是发布的内容,我看不出这如何符合XY问题。这段代码并不局限于那个上下文,那只是目前的上下文。