0
Public Function Validate(updatedDetailList As List(Of DetailVO)) As Boolean
  Dim matchFound As Boolean = False

  For Each firstUpdatedDetail In updatedDetailList
    For Each nextUpdatedDetail In updatedDetailList
      If firstUpdatedDetail.PROD_ID.Equals(nextUpdatedDetail.PROD_ID) Then
        matchFound = True
      End If
    Next nextUpdatedDetail
  Next firstUpdatedDetail

  Return matchFound
End Function

我有updatedDetailList一个列表,我想迭代并获取当前和下一个对象值并比较这两个值。如果您发现相同PROD_IDupdatedDetailList则返回matchFoundTRUE

有什么方法可以在内部 For 循环中获取下一个对象。像...

For Each firstUpdatedDetail In **updatedDetailList**
  For Each nextUpdatedDetail In **updatedDetailList.Next**
    If firstUpdatedDetail.PROD_ID.Equals(nextUpdatedDetail.PROD_ID) Then
      matchFound = True
    End If
  Next nextUpdatedDetail
Next firstUpdatedDetail
4

1 回答 1

1

似乎您正在尝试执行不同的验证,因此每个项目都updatedDetailList必须是唯一的。

在不改变你的方法(即使用For循环)的情况下,这里是代码:

For i = 0 to updatedDetailList.Count - 2
  If updatedDetailList(i).PROD_ID.Equals(updatedDetailList(i+1).PROD_ID) Then
    matchFound = True
    Exit For
  End If
Next

但是有一种更快的方法来执行相同的操作 - 它使用 LINQ:

Dim matchFound As Boolean = updatedDetailList.Distinct.Count <> updatedDetailList.Count
于 2012-10-30T23:28:30.130 回答