Answered How to Gruop By in DataTable {VB.Net}

  • Friday, May 11, 2012 4:31 PM
     
     

    Hi All

    I Have a DataTable In My DataSet

    SELECT * FROM DataTable:

    Result Is:

    Id        Name      FirstCredit        FirstDebit     Debit       Credit

    1           Bob             25                   0                 0             0

    1           Bob              0                    0                 50            0

    2           Jack              0                    80               10            20

    2           Jack               50                  0                 0             10

    Now I Want This Result Next Gruop By And Summerize in My DataTable Or New DataTable

    Id        Name      FirstCredit        FirstDebit     Debit       Credit

    1           Bob             25                    0               50            0

    2           Jack             50                   80              10            30

    Please Help Me

    Thanks

     


    Name of Allah, Most Gracious, Most Merciful and He created the human

All Replies

  • Friday, May 11, 2012 6:22 PM
     
     Answered

    Hi ,

    check the below sample code on Group By uising LINQ

    http://www.aspnet-answers.com/microsoft/Csharp/32180980/how-group-by-and-sum-values-in-datatable.aspx


    PS.Shakeer Hussain

  • Saturday, May 12, 2012 3:40 PM
     
     
    Please Sample Code By VB.Net thanks

    Name of Allah, Most Gracious, Most Merciful and He created the human

  • Sunday, May 13, 2012 4:46 AM
     
     

    Here's a decent online conversion tool:

    http://www.developerfusion.com/tools/convert/csharp-to-vb/


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • Monday, May 14, 2012 6:25 AM
    Moderator
     
     Answered Has Code

    Hi sh2013,

    Welcome to MSDN Forum.

    I have converted the demo code to VB, please refer to it.

    Imports System.Data
    Imports System.Linq
    
    Class Program
    	Private Shared Sub Main(args As String())
    		Dim table As New DataTable()
    		table.Columns.Add("Id", GetType(Integer))
    		table.Columns.Add("Value", GetType(Decimal))
    		table.Rows.Add(123, 4.0D)
    		table.Rows.Add(123, 5.0D)
    		table.Rows.Add(234, 1.0D)
    		table.Rows.Add(345, 2.0D)
    		table.Rows.Add(345, 3.0D)
    
    		Dim query = From grp In From row In table.AsEnumerable()Group row By row.Field(Of Integer)("Id")Order By grp.KeyNew With { _
    			Key .Id = grp.Key, _
    			Key .Sum = grp.Sum(Function(r) r.Field(Of Decimal)("Value")) _
    		}
    
    		For Each grp As var In query
    			Console.WriteLine("{0}" & vbTab & "{1}", grp.Id, grp.Sum)
    		Next
    	End Sub
    End Class
    

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us