我想在VB.NET或C#中編寫Linq子句的代碼,該代碼返回所有記錄,直到其中一個列值的總和達到某個值並分組爲止由另一列..獲取列/字段總和等於某個值的行
現在我有一個在MySQL條款:
SELECT
O.Id,
O.Supp,
O.TotalCol,
(SELECT
sum(TotalCol) FROM Table1
WHERE Id <= O.Id) 'bTotal'
FROM Table1 O
HAVING bTotal <= 50000
代碼我已經做了:
對象類:
Class Invoices
Public id As Integer
Public supplier As String
Public total As Decimal
Public Sub New(ByVal id As Integer, ByVal supplier As String, ByVal total As Decimal)
Me.id = id
Me.supplier = supplier
Me.total = total
End Sub
End Class
的未分組列表:
Dim LInvoicesPrincipal As New List(Of Invoices)()
Dim InvoiceItem As Invoices
InvoiceItem = New Invoices(1, "SUPP 1", 45000)
LInvoicesPrincipal.Add(InvoiceItem)
InvoiceItem = New Invoices(2, "SUPP 1", 6000)
LInvoicesPrincipal.Add(InvoiceItem)
InvoiceItem = New Invoices(3, "SUPP 2", 15000)
LInvoicesPrincipal.Add(InvoiceItem)
InvoiceItem = New Invoices(4, "SUPP 2", 6000)
LInvoicesPrincipal.Add(InvoiceItem)
InvoiceItem = New Invoices(5, "SUPP 1", 4000)
LInvoicesPrincipal.Add(InvoiceItem)
在LINQ分組,由提供商(我需要限制的總數的總和到一定值的規則(50000要準確)):
Dim LGroups
LGroups = _
From oInvoices In LInvoicesPrincipal _
Group oInvoices By oInvoices.supplier _
Into Group Select Group
而且一個For Each,讓我來驗證分組:
For Each EachList As Invoices() In LGroups
For Each EachItem As Invoices In EachList
Next
Next
這一切現在返回我列出這樣的:
Group 1:
Invoices(1, "SUPP 1", 45000)
Invoices(2, "SUPP 1", 6000)
Invoices(5, "SUPP 1", 4000)
Group 2:
Invoices(3, "SUPP 2", 15000)
Invoices(4, "SUPP 2", 6000)
但是......我想是這樣的:
Group 1:
Invoices(1, "SUPP 1", 45000)
Invoices(5, "SUPP 1", 4000)
Group 2:
Invoices(2, "SUPP 1", 6000)
Group 3:
Invoices(3, "SUPP 2", 15000)
Invoices(4, "SUPP 2", 6000)
(值的總和必須< = 50000每組)
請提供您已經完成的一些安排和代碼。 – kat1330
你最後一個(刪除)問題,它更多的是一個組合的東西或一個最佳適合算法。我不確定你可以在SQL – Plutonix
中做任何事情也許這是兩者的結合......我已經在腦海中循環着同樣的想法,我需要一個喘息的機會...... – Glegan