Sumit Kathuria
Sumit Kathuria

Reputation: 11

Running SQL Against a DataTable

I have pragmatically 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 e.g.:

select ID, sum(Rate) 
from **dataTable** 
group by ID

Upvotes: 0

Views: 851

Answers (1)

Hanlet Escaño
Hanlet Escaño

Reputation: 17370

You cannot re-write SQL against the Datatable, but you can use the DataTable.DefaultView.RowFilter, DataTable.DefaultView.Sort string properties to further filter your current set of data:

Example from MSDN:

using System;
using System.Data;
using System.Windows.Forms;

public class Form1 : Form {
   protected TextBox Text1;
   protected DataSet DataSet1;

   public static void Main() {
      DemostrateDataView();
   }

   private static void DemostrateDataView() {
      // Create a DataTable with one column
      DataTable dt = new DataTable("MyTable");
      DataColumn column = new DataColumn("Col", typeof(int));
      dt.Columns.Add(column); 

      // Add 5 rows on Added state 
      for (int i = 0; i < 5; i++) {
         DataRow row = dt.NewRow();
         row["Col"] = i;
         dt.Rows.Add(row);
      }

      // Add 5 rows on Unchanged state 
      for (int i = 5; i < 10; i++) {
         DataRow row = dt.NewRow();
         row["Col"] = i;
         dt.Rows.Add(row);
         // Calling AcceptChanges will make the DataRowVersion change from Added to Unchanged in this case
         row.AcceptChanges();
      }

      // Create a DataView
      DataView dv = new DataView(dt);

      Console.WriteLine("Print unsorted, unfiltered DataView");
      PrintDataView(dv);

      // Changing the Sort order to descending
      dv.Sort = "Col DESC";

      Console.WriteLine("Print sorted DataView. Sort = 'Col DESC'");
      PrintDataView(dv);

      // Filter by an expression. Filter all rows where column 'Col' have values greater or equal than 3
      dv.RowFilter = "Col < 3";

      Console.WriteLine("Print sorted and Filtered DataView by RowFilter. RowFilter = 'Col > 3'");
      PrintDataView(dv);

      // Removing Sort and RpwFilter to ilustrate RowStateFilter. DataView should contain all 10 rows back in the original order
      dv.Sort = String.Empty;
      dv.RowFilter = String.Empty;

      // Show only Unchanged rows or last 5 rows
      dv.RowStateFilter = DataViewRowState.Unchanged;

      Console.WriteLine("Print Filtered DataView by RowState. RowStateFilter =  DataViewRowState.Unchanged");
      PrintDataView(dv);
   }

   private static void PrintDataView(DataView dv) {
      // Printing first DataRowView to demo that the row in the first index of the DataView changes depending on sort and filters
      Console.WriteLine("First DataRowView value is '{0}'", dv[0]["Col"]);

      // Printing all DataRowViews 
      foreach (DataRowView drv in dv) {
         Console.WriteLine("\t {0}", drv["Col"]);
      }
   }
}

More info: http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

Datatable Group By: https://stackoverflow.com/a/8472044/752527

Upvotes: 1

Related Questions