2010/07/08

Distinct in DataTable or DataView

How to remove duplicated data? Suppose I have a DataTable (dtMembers) looks like this:
IDNameCityZipCode
01KennyLA12345
02PeterCA54321
03JohnNY13125
04JimmyNY13125







Using the following two lines of codes if you want to get the non-duplicated city list:
string[] columnNames = new string[] { "City" };
DataTable dtCity = dtMembers.DefaultView.ToTable(true, columnNames);
The result will be "LA, CA, NY".

Take a look of the DataView.ToTable() method and this discussion.