【问题标题】:DataTable select rows where debit and credits matchDataTable 选择借方和贷方匹配的行
【发布时间】:2013-10-15 00:12:04
【问题描述】:

DataTable 具有以下列的位置

帐号(整数)(唯一)

借方(十进制)

信用(十进制)

选中(布尔)

可能有几行具有相同的帐号和借记分录或贷记分录。我试图做的是匹配借方和贷方,它们加起来相同的值,并通过遍历所有行将它们标记为选中。对实现这一目标的最佳方法有任何想法吗?

谢谢

例如

SQL

strSQL = "SELECT A_Sales_Ledger.Transaction_ID as 'Transaction', "
        strSQL += "Customers.Cust_No as 'Acct', "
        strSQL += "Customers.Cust_Name as 'Name', "
        strSQL += "Customers.Add1 as 'Unit', "
        strSQL += "A_Sales_Ledger.Debit as 'Debit', "
        strSQL += "A_Sales_Ledger.Credit as 'Credit', "
        strSQL += "A_Sales_Ledger.Document_Date as 'Date', "
        strSQL += "A_Sales_Ledger.S_Description as 'Description' "
        strSQL += "FROM A_Sales_Ledger "
        strSQL += "JOIN Customers ON Customers.Customer_ID = A_Sales_Ledger.Customer_ID "
        strSQL += "WHERE A_Sales_Ledger.Paid = 'N' "
        strSQL += "ORDER BY Customers.Cust_No"

【问题讨论】:

  • 您是否尝试过使用 linq 查询将表连接到帐户 # 上的自身,其中贷方等于借方
  • 嘿康拉德 - 你能发布一个例子吗?我没有遇到帐号的所有行都匹配的问题,即 SUM(Credit) = SUM(Debit).. 谢谢

标签: wpf datagrid datatable


【解决方案1】:

这可能不是最好的解决方案,但它似乎确实有效......

我正在通过 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多