Results 1 to 5 of 5

Thread: Vb linq help group by and sum

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2012
    Posts
    19

    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 >

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2012
    Posts
    19

    Re: Vb linq help group by and sum

    I cant use nomral sql in lightswitch
    < advertising removed by moderator >

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Vb linq help group by and sum

    Quote Originally Posted by jbaillie View Post
    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!

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Vb linq help group by and sum

    it's not SQL. it's LINQ to query a datatable, with grouping

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width