making rows distinct and showing all the columns

Mr_Green picture Mr_Green · Oct 20, 2012 · Viewed 11.1k times · Source

In my project there are two datatables dtFail and dtFailed (dtFailed has nothing but column names declarations). dtFail has duplicate "EmployeeName" column values. so i took a dataview dvFail and did the process to make them distinct as shown in the below code:

dtFail

enter image description here

I tried the below code:

   DataView dvFail = new DataView(dtFail);
   dtFail = dvFail.ToTable(true, "EmployeeName"); //showing only one column in dtFail

dtFailed (only one column)

enter image description here

If i do like below

   DataView dvFail = new DataView(dtFail);
   dtFail = dvFail.ToTable(true, "EmployeeName","EmployeeRole","Status");

dtFailed (showing but with duplicate rows)

enter image description here

Then the datatable dtFailed is storing duplicate "EmployeeName" also.

Please Help
Thanks in Advance.

Answer

ShaileshDev picture ShaileshDev · Jun 29, 2015

Try this query-

DataTable distinctTable = originalTable.DefaultView.ToTable( /*distinct*/ true);

For more info hit below link-

https://social.msdn.microsoft.com/Forums/en-US/ed9c6a6a-a93e-4bf5-a892-d8471b84aa3b/distinct-in-datatable-or-dataview?forum=adodotnetdataset

I hope this would have helped you.