Wednesday, June 09, 2010

Select distinct rows and sum columns in DataTable

How to filter distinct row from a DataTable column?
Use the [DataView].ToTable (bool, string[]) method. The following code returns a distinct category (no repeated rows in same category).

Example:
DataTable newTable = view.ToTable(true, "Category");


How to sum column total from a DataTable?
Use the DataTable Compute () function. This method performs aggregate function on the DataTable based on the filter criteria.

Example:
newTable.Compute("Sum(Sales)","Category='Pasta'");

No comments: