2013-10-15 39 views
0

如果一個DataTable具有以下的列數據表中選擇行其中借方和貸方匹配

賬號(整數)(唯一)

借記(十進制)

信貸(十進制)

Selected(布爾型)

可能有幾行具有相同帳號和借項或貸記條目。我試圖做的是匹配借方和學分,他們加起來相同的價值,並通過遍歷所有行標記爲選中。任何想法來實現這個最好的方法?

謝謝

例如,

DateTable (in WPF DataGrid)

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" 
+0

您是否嘗試過使用LINQ查詢加入表本身的帳號和地方信用等於借記 –

+0

嘿康拉德 - 你能張貼的例子嗎?我沒有問題的帳戶號碼匹配的所有行,即SUM(信用)= SUM(借記)..謝謝 – gchq

回答

0

這可能不是最好的解決方案,但它確實工作...

我做三道雖然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