Vb linq help group by and sum
I trying to do the following in linq
SELECT SUM(qty) AS Expr1, tblOrderDetails_tblProduct, tblOrderDetails_tblOrders
FROM tblOrderDetailsSet
where Planno = 1
GROUP BY tblOrderDetails_tblProduct, tblOrderDetails_tblOrders
Trying to make this work in Linq Please help
Dim dw = Me.Application.CreateDataWorkspace
Dim Query =
From c In dw.ApplicationData.tblOrderDetailsSet _
Where c.PlanNo = 1 _
Order By c.ProductId Ascending _
Group productid By qtytest = c.qty Into qtyTotal = Group
Select New With {.Product = Product, .qty = qtyTotal.Sum()}
Re: Vb linq help group by and sum
here's an example of LINQ grouping, ordering + summing:
Code:
Public Class Form1
Dim r As New Random
Dim products() As String = {"product1", "product2", "product3", "product4", "product5"}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tblOrderDetailsSet As New DataTable
tblOrderDetailsSet.Columns.Add("PlanNo", GetType(Integer))
tblOrderDetailsSet.Columns.Add("ProductId", GetType(Integer))
tblOrderDetailsSet.Columns.Add("Product")
tblOrderDetailsSet.Columns.Add("qty", GetType(Integer))
For x As Integer = 1 To 100
tblOrderDetailsSet.Rows.Add(r.Next(1, 4), r.Next(1, 10001), products(r.Next(0, 5)), r.Next(1, 101))
Next
Dim Query = _
From c As DataRow In tblOrderDetailsSet.Rows.Cast(Of DataRow)() _
Where c.Field(Of Integer)("PlanNo") = 1 _
Order By c.Field(Of Integer)("ProductId") Ascending _
Group c By Product = c.Field(Of String)("Product") Into Group _
Select New With {.Product = Product, .qty = Group.Sum(Function(r As DataRow) r.Field(Of Integer)("qty"))}
Stop
End Sub
End Class
Re: Vb linq help group by and sum
I cant use nomral sql in lightswitch
Re: Vb linq help group by and sum
Quote:
Originally Posted by
jbaillie
I cant use nomral sql in lightswitch
And? You asked for LINQ solution and you were given a LINQ solution. There's no SQL involved (unless my eye deceive me!)
Re: Vb linq help group by and sum
it's not SQL. it's LINQ to query a datatable, with grouping