Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. DataTable: Query a programmatically generated DataTable

DataTable: Query a programmatically generated DataTable

Scheduled Pinned Locked Moved ASP.NET
databasequestioncsharpasp-net
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sumit Kathuria
    wrote on last edited by
    #1

    I have programmatically generated a DataTable in ASP.NET 3.5 and now I want to use group by and do some calculations using SQL on this DataTable. My question is: Is it possible to write a new SQL query against this DataTable and generate a new updated DataTable? For eg: select ID,sum(Rate) from dataTable group by ID

    Sumit Kathuria

    D M 2 Replies Last reply
    0
    • S Sumit Kathuria

      I have programmatically generated a DataTable in ASP.NET 3.5 and now I want to use group by and do some calculations using SQL on this DataTable. My question is: Is it possible to write a new SQL query against this DataTable and generate a new updated DataTable? For eg: select ID,sum(Rate) from dataTable group by ID

      Sumit Kathuria

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      I've never seen it done. You could push the datatable up to the SQL server in a #TEMP table, then do your SQL from there. Not necessarily the best way to do things, but it would work.

      1 Reply Last reply
      0
      • S Sumit Kathuria

        I have programmatically generated a DataTable in ASP.NET 3.5 and now I want to use group by and do some calculations using SQL on this DataTable. My question is: Is it possible to write a new SQL query against this DataTable and generate a new updated DataTable? For eg: select ID,sum(Rate) from dataTable group by ID

        Sumit Kathuria

        M Offline
        M Offline
        mr priyank
        wrote on last edited by
        #3

        You can use Linq Or Lamda Expression to form such query

        DataTable dt = new DataTable();

                dt.Columns.Add("ID");
                dt.Columns.Add("Rate");
                dt.Rows.Add("1", "10");
                dt.Rows.Add("1", "20");
                dt.Rows.Add("2", "30");
                dt.Rows.Add("2", "40");
        
                var result = dt.AsEnumerable().GroupBy(row => row\["ID"\]).Select(GroupedRows => new { ID = GroupedRows.Key, SumOfRate = GroupedRows.Sum(row => Convert.ToDecimal(row\["Rate"\])) }).ToList();
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups