VB.Net 数据表循环建议

VB.Net Datatable Loop Suggestion

我有一个数据表,其查询已分组,因此我的所有记录和紧接的下一条记录都包含相关记录。然而,在某些情况下,下一条记录可能与前一条记录无关。

我的场景:

数据包含两条相关记录,第一条记录用户入场日期和时间,下一条记录用户超时日期和时间。

我的任务是检测用户超时,并进一步处理。

我想知道最好的方法是什么。

这是我当前的代码。

Dim i as Integer = 0

For Each ucdr in ucdt.Rows
if ucdr(i) = ucdr(i+1) Then
'Do something 
End If
Next

但是 'i' 总是会在循环中发生变化,所以我想知道如何最好地修复这个循环?

据我了解,您想知道这一行的 UserTimeIn 列是否包含与下一行的 UserTimeOut 列相同的值。

您必须使用 For 循环通过行索引访问行:

For i As Int32 = 0 To ucdt.Rows.Count - 1
    Dim thisRow = ucdt.Rows(i)
    Dim nextRow = If(i = ucdt.Rows.Count - 1, Nothing, ucdt.Rows(i + 1))
    If nextRow IsNot Nothing Then
        Dim userTimeIn = thisRow.Field(Of Date)("UserTimeIn")
        Dim userTimeOut = nextRow.Field(Of Date)("UserTimeOut")
        If userTimeIn = userTimeOut Then
            ' do whatever you need to do '
        End If
    End If
Next