这可能不是最好的解决方案,但它似乎确实有效......
我正在通过 DataTable 进行三遍
通过一 - 如果借方的总和等于每个客户的贷方的总和,则将它们标记为选中
通过二 - 添加客户的总积分并遍历借方,直到达到相同的值并将它们标记为选中
通过三 - 清除与同一客户的借记条目匹配的任何剩余贷记条目......
For Each Row As DataRow In BalanceDT.Rows
Dim vAcct As String = Row("Acct")
Dim vDebits As Decimal = BalanceDT.Compute("SUM(Debit)", "Acct = '" & vAcct & "'")
Dim vCredits As Decimal = BalanceDT.Compute("SUM(Credit)", "Acct = '" & vAcct & "'")
If vDebits = vCredits Then
Row("Selected") = True
End If
Next
Dim CurrentCust As String = ""
Dim TransactionDT As New DataTable
Dim ExitFor As Boolean = False
With TransactionDT.Columns
.Add("ID", GetType(Integer))
End With
For Each Row As DataRow In BalanceDT.Rows
Dim vAcct As String = Row("Acct")
ExitFor = False
TransactionDT.Rows.Clear()
If Not CurrentCust = vAcct Then
Dim vCredits As Decimal = BalanceDT.Compute("SUM(Credit)", "Acct = '" & vAcct & "'")
Dim vSelected() As DataRow = BalanceDT.Select("Acct = '" & vAcct & "'", Nothing)
For Each SubRow As DataRow In vSelected
If SubRow("Selected") = False Then
vCredits -= SubRow("Debit")
With TransactionDT.Rows
.Add(SubRow("Transaction"))
End With
If vCredits = 0 Then
ExitFor = True
Exit For
End If
End If
Next
If ExitFor = True Then
For Each SubRow As DataRow In vSelected
If SubRow("Credit") > 0 Then
SubRow("Selected") = True
End If
If SubRow("Debit") > 0 Then
For Each CustomerRow As DataRow In TransactionDT.Rows
If CustomerRow("ID") = SubRow("Transaction") Then
SubRow("Selected") = True
End If
Next
End If
Next
End If
End If
CurrentCust = vAcct
Next
For Each Row As DataRow In BalanceDT.Rows
Dim vAcct As String = Row("Acct")
If Not CurrentCust = vAcct Then
Dim vSelected() As DataRow = BalanceDT.Select("Acct = '" & vAcct & "' AND Selected = 'False'", "Credit")
For Each SubRow As DataRow In vSelected
If SubRow("Selected") = False Then
Dim vCredit As Decimal = SubRow("Credit")
For Each CustomerRow In vSelected
If CustomerRow("Selected") = False Then
Dim vDebit As Decimal = CustomerRow("Debit")
If Not vDebit = 0 And Not vCredit = 0 Then
If vDebit = vCredit Then
With TransactionDT.Rows
CustomerRow("Selected") = True
SubRow("Selected") = True
End With
Exit For
End If
End If
End If
Next
End If
Next
End If
CurrentCust = vAcct
Next