none
How to Gruop By in DataTable {VB.Net} RRS feed

  • Question

  • 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

    Friday, May 11, 2012 4:31 PM

Answers

  • 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

    Friday, May 11, 2012 6:22 PM
  • 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

    Monday, May 14, 2012 6:25 AM
    Moderator

All replies

  • 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

    Friday, May 11, 2012 6:22 PM
  • Please Sample Code By VB.Net thanks

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

    Saturday, May 12, 2012 3:40 PM
  • 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

    Sunday, May 13, 2012 4:46 AM
  • 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

    Monday, May 14, 2012 6:25 AM
    Moderator
  • I duplicated the code above but i get the following error in  row.Field(Of Integer)("Id")Order .

    the error is Range variable name can be inferred only from a simple Or qualified name With no arguments

     

    JrCharlie

    Sunday, October 25, 2015 6:55 PM
  • Code will Not compile.


    Mary

    Friday, April 12, 2019 6:12 PM