Is it possible to loop through a filtered data table and update the items as needed?
In my ‘For i As..Next’ loop, I want to check the value of a specific column in the next row and if they’re not equal to the column in the current row, perform some calculations. I’ve tried using this approach in a ‘For Each’ loop, but I don’t believe it’s possible? Also, I’m trying to only loop through records I care about (specifically the ones that result from my SelectStatement parameter) like I’m doing with my first ‘For Each’ loop.
Public Shared Function MyFunction(ByRef TimeDataTable As DataTable, ByRef SelectStatement As String) As Boolean
Try
Dim Units As Decimal ' units (hours) billed for this day
Dim Hours As Decimal = 0.00 ' total hours minus any PTO
Dim ProratePercent As Decimal = 0.00 ' calculated percentage; 40+ hours
Dim NewUnits As Decimal = 0.00 ' new hours after calculating with ProratePercent
' prorate employee's hours for the given week
For Each TimeRow As DataRow In TimeDataTable.Select(SelectStatement)
Hours = (TimeRow.Item("TotalHours") - TimeRow.Item("PTOHours"))
Units = (TimeRow.Item("Units"))
ProratePercent = CDec(Units / Hours)
NewUnits = (ProratePercent * (40 - TimeRow.Item("PTOHours")))
TimeRow.Item("Units") = Math.Round(CDec(NewUnits), 2)
'remove this before commiting to production; just to mark that this was manipulated in the import file for data comparison
TimeRow.Item("Time Off Name") = TimeRow.Item("Time Off Name") & " (Prorated)"
Hours = 0.00
ProratePercent = 0.00
NewUnits = 0.00
Next
' check previous usernames; if they don't match, check if last value adds total prorated hours to 40
For i As Integer = 0 To TimeDataTable.Rows.Count - 1
Dim TimeRow As DataRow = TimeDataTable.Rows(i)
Dim Username As String = TimeRow("Username").ToString()
If i <> TimeDataTable.Rows.Count - 1 Then
Dim nextRow As DataRow = TimeDataTable(i + 1)
Dim NextUsername As String = nextRow("Username").ToString()
If Username <> NextUsername And (TimeRow("TotalHours") > 40) Then
' adjust the final record to equal 40 hours
End If
End If
Next
Catch ex As Exception
_logger.Error("ProrateUnits function error: " & ex.Message.ToString())
Return False
End Try
Return True
End Function
I don’t like how I’m resorting to looping through every single record in the data table in this last ‘For Next’ loop.
Visual Studio skips over my logic whenever I try to use this:
For i As Integer = 0 To TimeDataTable.Select(SelectStatement).Count - 1
Dim TimeRow As DataRow = TimeDataTable.Rows(i)
Dim Username As String = TimeRow("Username").ToString()
If i <> TimeDataTable.Select(SelectStatement).Count - 1 Then
Dim nextRow As DataRow = TimeDataTable(i + 1)
Dim NextUsername As String = nextRow("Username").ToString()
If Username <> NextUsername And (TimeRow("TotalHours") > 40) Then
' adjust the final record to equal 40 hours
End If
End If
Next
Candace Aisenbrey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.