I'm trying to use Linq groupby and where with date conditions in vb.net
Please Guide Me.
PIQ is total purchase quantity
SIQ is total sales quantity
BLC is total purchase quantity minus total sales quantity
Thanks
Public Class Form3 Private Purchase, Sales As New List(Of Invoice) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load LoadData() End Sub Private PurchaseDetails,SalesDetails As New List(Of Detail) Private Function CreateConnection() As String Return ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\TRIAL2.accdb;Persist Security Info=False;") End FunctionPrivate Sub LoadData() Using Connection = New OleDbConnection(CreateConnection()) Purchase = CType(Connection.Query(Of Invoice)("SELECT * FROM PURCHASE"), List(Of Invoice)) PurchaseDetails = CType(Connection.Query(Of Detail)("SELECT * FROM PURCHASEDETAILS"), List(Of Detail)) Sales = CType(Connection.Query(Of Invoice)("SELECT * FROM SALES"), List(Of Invoice)) SalesDetails = CType(Connection.Query(Of Detail)("SELECT * FROM SALESDETAILS"), List(Of Detail))End Using Dim ps = From p In Purchase Where p.DATEINVO >= CDate("01/01/2024") From pd In PurchaseDetails Select pd.ITEM, PIQ = pd.QTY, SIQ = 0, BLC = pd.QTY Order By ITEM Dim ss = From s In Sales Where s.DATEINVO >= CDate("01/01/2024") From sd In SalesDetails Select sd.ITEM, PIQ = 0, SIQ = sd.QTY, BLC = -sd.QTY Order By ITEM Dim Card_temp = ps.Union(ss).OrderBy(Function(w) w.ITEM) Dim Card As New List(Of ItemCards3) Dim RunningBalance As Integer = 0 For Each ct In Card_temp Dim sc As New ItemCards3 With sc .ITEM = ct.ITEM .PIQ = ct.PIQ .SIQ = ct.SIQ .BLC = RunningBalance + ct.BLC RunningBalance = .BLC End With Card.Add(sc) Next DataGridView1.DataSource = Card End SubEnd ClassPublic Class Invoice Property INVONO() As String Property DATEINVO() As Date Property CUSTCODE() As StringEnd ClassPublic Class Detail Public Property INVONO() As String Public Property NOD() As Integer Public Property ITEM() As String Public Property QTY() As IntegerEnd ClassPublic Class ItemCards3 Public Property ITEM() As String Public Property PIQ As Integer Public Property SIQ As Integer Public Property BLC As IntegerEnd Class
Table PURCHASE
INVONO | DATEINVO | CUSTCODE |
---|---|---|
PI1000 | 25-Jan-23 | 001 |
PI1001 | 29-Jan-23 | 002 |
PI1002 | 29-Jan-24 | 003 |
PI0003 | 30-Jan-24 | 004 |
Table PURCHASEDETAILS
INVONO | NOD | ITEM | QTY |
---|---|---|---|
PI1000 | 1 | TEST 1000 | 10 |
PI1000 | 2 | TEST 2000 | 20 |
PI1001 | 1 | TEST 3000 | 15 |
PI1002 | 1 | TEST 1000 | 20 |
PI1003 | 1 | TEST 1000 | 15 |
Table SALES
INVONO | DATEINVO | CUSTCODE |
---|---|---|
SI1000 | 25-Jan-23 | 001 |
SI1001 | 29-Jan-23 | 002 |
SI1002 | 29-Jan-24 | 005 |
SI1003 | 30-Jan-24 | 006 |
SI1004 | 29-Jan-24 | 004 |
SI1005 | 30-Jan-24 | 003 |
Table SALESDETAILS
INVONO | NOD | ITEM | QTY |
---|---|---|---|
SI1000 | 1 | TEST 1000 | 10 |
SI1000 | 2 | TEST 2000 | 20 |
SI1001 | 1 | TEST 8000 | 35 |
SI1002 | 1 | TEST 1000 | 27 |
SI1003 | 1 | TEST 1000 | 15 |
SI1004 | 1 | TEST 2000 | 2 |
SI1005 | 1 | TEST 2000 | 2 |
the result of the code above
ITEM | PIQ | SIQ | BLC |
---|---|---|---|
TEST 1000 | 15 | 0 | 15 |
TEST 1000 | 10 | 0 | 25 |
TEST 1000 | 20 | 0 | 45 |
TEST 1000 | 0 | 10 | 35 |
TEST 1000 | 0 | 27 | 8 |
TEST 1000 | 0 | 15 | -7 |
TEST 2000 | 20 | 0 | 13 |
TEST 2000 | 0 | 20 | -7 |
TEST 2000 | 0 | 2 | -9 |
TEST 3000 | 15 | 0 | 6 |
TEST 8000 | 0 | 35 | -29 |
Desired result
ITEM | PIQ | SIQ | BLC |
---|---|---|---|
TEST 1000 | 35 | 42 | -7 |
TEST 2000 | 4 | -4 |