Results 1 to 5 of 5

Thread: LINQ Group and Sum

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    LINQ Group and Sum

    Hi, all masters.

    I have a problem. I use below code;

    Dim dt = New DataTable("tblEntTable")
    ...
    ...
    dt.Columns.Add("ID", GetType(String))
    dt.Columns.Add("Amount", GetType(Decimal))

    dt.Rows.Add
    .....
    .....
    Dim res = dt.AsEnumerable()
    .OrderBy(Function(row) row.Field(Of String)("ID"))
    .GroupBy(Function(row) row.Field(Of String)("ID"))
    .[Select](Function(grp) grp.First())

    dtgrdClone.DataSource = res.CopyToDataTable


    Code is perfect run, but not SUM Amount Fields.

    How to SUM "Ammunt" Fields.

    Please Help

    Kind Regards Gokhan

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: LINQ Group and Sum

    Hi,

    I believe you want to group the id's and sum all the amounts for that particular id. Here's how i did it using LINQ.

    Code:
     Dim result = (From orders In dt.AsEnumerable
                            Group orders By ID = orders.Field(Of String)("ID") Into g = Group
                            Select New With {
                                Key ID,
                                .Amount = g.Sum(Function(r) r.Field(Of Decimal)("amount"))
                           }).OrderBy(Function(tkey) tkey.ID).ToList()
    
    dtgrdClone.DataSource = result
    Last edited by KGComputers; Apr 9th, 2014 at 06:35 AM.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: LINQ Group and Sum

    KGComputers thank you to interest. You are a master,

    But there are too many columns in datatable. This code returns just two column "ID" and "amaount"

    Real datatable schema like this;

    dt.Columns.Add("DATE", GetType(Date))
    dt.Columns.Add("ID", GetType(String))
    dt.Columns.Add("INFORMATION", GetType(String))
    dt.Columns.Add("QTY", GetType(Decimal))
    dt.Columns.Add("AMOUNT", GetType(Decimal))
    dt.Columns.Add("empID", GetType(Short))
    bla
    bla
    bla

    Datatable columns total 60 fields.
    I don't want to write for a very long code
    What can i do sir.

    Thanks.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: LINQ Group and Sum

    KGComputers thank you to interest. You are a master,

    But there are too many columns in datatable. This code returns just two column "ID" and "amaount"

    Real datatable schema like this;

    dt.Columns.Add("DATE", GetType(Date))
    dt.Columns.Add("ID", GetType(String))
    dt.Columns.Add("INFORMATION", GetType(String))
    dt.Columns.Add("QTY", GetType(Decimal))
    dt.Columns.Add("AMOUNT", GetType(Decimal))
    dt.Columns.Add("empID", GetType(Short))
    bla
    bla
    bla

    Datatable columns total 60 fields.
    I don't want to write for a very long code
    What can i do sir.

    Thanks.

    Quote Originally Posted by KGComputers View Post
    Hi,

    I believe you want to group the id's and sum all the amounts for that particular id. Here's how i did it using LINQ.

    Code:
     Dim result = (From orders In dt.AsEnumerable
                            Group orders By ID = orders.Field(Of String)("ID") Into g = Group
                            Select New With {
                                Key ID,
                                .Amount = g.Sum(Function(r) r.Field(Of Decimal)("amount"))
                           }).OrderBy(Function(tkey) tkey.ID).ToList()
    
    dtgrdClone.DataSource = result

  5. #5
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: LINQ Group and Sum

    Datatable columns total 60 fields.
    Normally, a datatable does not contain lots of columns as what you have specified. How did you populate the datatable? Through a query?
    Or you just add several rows to the datatable at runtime using
    Code:
    dt.Rows.Add(....)
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Tags for this Thread

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