0

我已经在 ASP.NET 3.5 中务实地生成了一个DataTable,现在我想使用 group by 并在此上使用 SQL 进行一些计算DataTable

我的问题是:是否可以为此编写一个新的 SQL 查询DataTable并生成一个新的更新DataTable

例如:

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

1 回答 1

1

您不能针对 重写 SQL Datatable,但可以使用DataTable.DefaultView.RowFilter,DataTable.DefaultView.Sort字符串属性进一步过滤当前数据集:

来自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"]);
      }
   }
}

更多信息:http: //msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

数据表分组方式:https : //stackoverflow.com/a/8472044/752527

于 2013-03-28T18:35:34.237 回答