2016-05-17 53 views
0

enter image description here我對分組和suming列此代碼:Linq的分組和數據表塞汀

var groupedData = from b in showit.AsEnumerable() 
        group b by b.Field<string>("Key") into g 
        select new 
        { 
        KeyName = g.Key, 
        Calls_Chats_Answered = g.Sum(x => x.Field<int>("Calls_Chats_Answered")) 
        }; 

如何通過groupedData到數據表?

+0

這些鏈接之一的可能重複:1的最佳實踐:將LINQ查詢結果不循環的DataTable(http://stackoverflow.com/questions/4460654/best-practice-convert-linq -query-result-to-a-datatable-without-looping) – har07

+0

2. [如何將此LINQ查詢結果轉換回DataTable對象?](http://stackoverflow.com/questions/19143278/how-to-convert -this-linq-query-result-back-to-datatable-object) – har07

+0

應該在哪裏顯示分組數據?圖像沒有更清晰。 –

回答

0
 //Create DataTable 
     DataTable dt = new DataTable(); 

     //Add DataColumns to DataTable 
     dt.Columns.Add("KeyName", typeof(string)); 
     dt.Columns.Add("Calls_Chats_Answered", typeof(int)); 

     //Iterate over the query 
     foreach (var v in groupedData) 
     { 
      //Create new DataRow 
      DataRow dr = dt.NewRow(); 

      //Add values to DataRow 
      dr["KeyName"] = v.KeyName; 
      dr["Calls_Chats_Answered"] = v.Calls_Chats_Answered; 

      //Add DataRow to DataTable 
      dt.Rows.Add(dr); 
     }