0

enter image description hereI have this code for grouping and suming column:

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

How do I pass the groupedData to datatable ?

  • Possible duplicate of one of these links : 1. [Best Practice: Convert LINQ Query result to a DataTable without looping](http://stackoverflow.com/questions/4460654/best-practice-convert-linq-query-result-to-a-datatable-without-looping) – har07 May 17 '16 at 23:50
  • 2. [How to convert this LINQ Query result back to DataTable object?](http://stackoverflow.com/questions/19143278/how-to-convert-this-linq-query-result-back-to-datatable-object) – har07 May 17 '16 at 23:50
  • Where should the grouped data be displayed? The image doesn't make it any more clear. – Gert Arnold May 18 '16 at 15:10

1 Answers1

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);
        }
abeldenibus
  • 128
  • 7