比较 属性 的两个对象列表的每一行并创建新对象

Comparing every row of two lists of objects for a property and create new object

这是我正在尝试做的事情:

将 compareList 的每一行与 refList 的每一行进行比较

    ----- Do you find 'Leistungscode' of compareList in refList?
     ----- Yes--> Ignore
     ----- No--> New Entry, add to resutList

我的问题是,对于我的 for each 循环,每次 Leistungscode 不匹配时它都会创建一个新条目,但我需要先查看整个 refList 然后再查看如果我没有找到它,请将其添加到列表中。你知道怎么做吗?

这是我尝试过的:

For Each rowCompare In compareList
    For Each rowRef In refList
        If rowCompare.Leistungscode.CompareTo(rowRef.Leistungscode) = 0 Then

        Else
            resultList.Add(New ISAACService(rowCompare.Leistungscode, rowCompare.K_Art, rowCompare.UANR, rowCompare.Ueberbegriff, rowCompare.Benennung, rowCompare.Anzahl, rowCompare.Einheit, rowCompare.Einzelkosten, rowCompare.Summencode))
        End If
    Next
Next

首先我认为你不应该使用CompareToCompareTo 做额外的工作来确定位置。你只需要一个等号。

空的 If,代码只在 else 中,可以通过在 if 中添加 Not 来更正。

这里真正的关键是Exit For。一旦您找到一个不匹配的实例,该实例就会被添加到 resultList,因此我们希望在该点停止搜索以避免 resultList 中的重复条目。 Exit For 仅退出内部 For 循环。外循环将继续 commpareList.

的下一个元素
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each rowCompare In compareList
        For Each rowRef In refList
            If Not rowCompare.Leistungscode = rowRef.Leistungscode Then
                resultList.Add(New ISAACService(rowCompare.Leistungscode, rowCompare.K_Art, rowCompare.UANR, rowCompare.Ueberbegriff, rowCompare.Benennung, rowCompare.Anzahl, rowCompare.Einheit, rowCompare.Einzelkosten, rowCompare.Summencode))
                Exit For
            End If
        Next
    Next
    MessageBox.Show(resultList.Count.ToString)
End Sub