January 1, 2013

Distinct Rows from data table




I love the idea of keeping your data disconnected from your data source; dataset & data-table are a great way to manipulate your records and rows in memory…!!
Now let’s do this trick … getting the “Distinct” records from a data-table …Yes we can do that with one simple step …just pass the required columns to the “ToTable” method in the DefaultView …. as illustrated in the below code sample :


string[] Columns = {"IP","Country-Code"};
DataTable dtData = GetMyDataTableInfo();
DataTable distinctRecords = new DataTable();
distinctRecords = dtData.DefaultView.ToTable(true, Columns);


Please note that “GetMyDataTableInfo” is a method that will fill your data datable …but the impoatant part of these lines is “ToTable” …the second parameters “Columns” array of string contain the names of the columns that we need to accomplish the Distinct operations.


Happy programming ….