|
-
Apr 26th, 2013, 12:47 PM
#1
Thread Starter
Junior Member
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()}
< advertising removed by moderator >
-
Apr 26th, 2013, 02:06 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 26th, 2013, 02:20 PM
#3
Thread Starter
Junior Member
Re: Vb linq help group by and sum
I cant use nomral sql in lightswitch
< advertising removed by moderator >
-
Apr 26th, 2013, 02:25 PM
#4
Re: Vb linq help group by and sum
 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!)
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 26th, 2013, 02:26 PM
#5
Re: Vb linq help group by and sum
it's not SQL. it's LINQ to query a datatable, with grouping
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|