public DataTable GroupBy(string i_sGroupByColumn, string i_sAggregateColumn, DataTable i_dSourceTable) { DataView dv = new DataView(i_dSourceTable); //getting distinct values for group column DataTable dtGroup = dv.ToTable(true, new string[] { i_sGroupByColumn }); //adding column for the row count dtGroup.Columns.Add("Count", typeof(int)); //looping thru distinct values for the group, counting foreach (DataRow dr in dtGroup.Rows) { dr["Count"] = i_dSourceTable.Compute("Count(" + i_sAggregateColumn + ")", i_sGroupByColumn + " = '" + dr[i_sGroupByColumn] + "'"); } //returning grouped/counted result return dtGroup; }
Monday, January 28, 2013

Group by on Datatable with count function in c#
Subscribe to:
Post Comments (Atom)
Author Details
Md Mominul Islam
S/W Development Lead | Project Mgmt | DBA | Data Engineering
Microsoft-certified professional with 16+ years experience delivering 40+ enterprise solutions across diverse industries.
No comments:
Post a Comment
Thanks for your valuable comment...........
Md. Mominul Islam