1. 程式人生 > 其它 >DataTable 根據多列分組

DataTable 根據多列分組

      public static DataTable GetDataTable(DataTable dt)
        {

            Dictionary<string, DataTable> dict = new Dictionary<string, DataTable>();
            DataTable dtresDt = dt.Clone();

            DataView dv = new DataView(dt);
            DataTable dtTJ = dv.ToTable(true, "
band", "bw","scs", "ofdm", "modulation"); for (int i = 0; i < dtTJ.Rows.Count; i++) { //DataRow[] row = dt.Select("band='" + dtTJ.Rows[i]["band"].ToString() + "' and scs='" + dtTJ.Rows[i]["scs"].ToString() + " and bw='" + dtTJ.Rows[i]["bw"].ToString() + "' and ofdm='" + dtTJ.Rows[i]["ofdm"].ToString() + "' and modulation='" + dtTJ.Rows[i]["modulation"].ToString() + "'");
var res = dt.Select("band = '" + dtTJ.Rows[i]["band"].ToString() + "' and bw='" + dtTJ.Rows[i]["bw"].ToString() + "' and scs='" + dtTJ.Rows[i]["scs"].ToString() + "' and ofdm='" + dtTJ.Rows[i]["ofdm"].ToString() + "' and modulation='" + dtTJ.Rows[i]["modulation"].ToString() + "'");//按條件查詢出符合條件的行
DataTable resDt = dt.Clone();//克隆一個表 foreach (var j in res) { resDt.ImportRow(j);//將符合條件的行放進表裡 } //DataView dataView = new DataView(resDt); //dataView.Sort = "channel Asc"; // DataTable dtNew= dataView.ToTable();//根據channel 列排序 dict.Add(mode, resDt);//將鍵值對存起來 } foreach (var item in dict) { DataRow row = item.Value.Rows[0]; row["test_mode_data"] = item.Key; dtresDt.ImportRow(row); } return dtresDt; }