Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement a datatable "group by"?

Tags:

c#

datatable

I would like to implement a "Group By" for my datatable. Has any one any suggestions?

update:

c#, .net 2.0

like image 567
Brad Avatar asked Nov 17 '09 16:11

Brad


People also ask

How do you group data in a DataTable?

The grouping indicator is added by the drawCallback function, which will parse through the rows which are displayed, and enter a grouping TR element where a new group is found. A click event handler is added for the grouping rows to allow the grouping order to be restored as well as ordering by any other column.

Can Linq be used on DataTable?

A DataTable can be queried with LINQ, just as any other IEnumerable<T> list. Note: DataTable does not implement IEnumerable<T>. You have to call AsEnumerable, which is an extension method for DataTable, to obtain a wrapper that implements that interface.

How add data row to DataTable?

After you create a DataTable and define its structure using columns and constraints, you can add new rows of data to the table. To add a new row, declare a new variable as type DataRow. A new DataRow object is returned when you call the NewRow method.


1 Answers

You can use the linq extensions in the System.Data.DataSetExtensions assembly:

DataTable t = //
var groups = t.AsEnumerable()
    .GroupBy(r => r.Field<T>("columnName"))
like image 156
Lee Avatar answered Oct 04 '22 12:10

Lee