12

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.

Mr_Green
  • 40,727
  • 45
  • 159
  • 271

3 Answers3

7

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.

ShaileshDev
  • 1,086
  • 13
  • 16
1

SOLUTION 1:

Based on the question my understanding is, we need to consider duplicates based on EmployeeName and we need not worry about other columns. If that is the case below solution works better.

foreach(DataRow r in dtFail.AsEnumerable())
  {
   if (!dt1.AsEnumerable().Any(r1 => r1["EmployeeName"] == r["EmployeeName"]))
      {
     // if you don't want to copy entire row create new DataRow 
     // with required fields and add that row.
      dt1.Rows.Add(r.ItemArray);
      }
  }

if you want you can put dt1 back to dtFail.

SOLUTION 2:

If we need to consider distinct rows I prefer below solution.

var temp = dtFail.AsEnumerable().Distinct();
dtFail = temp.CopyToDataTable();
Raj Karri
  • 551
  • 4
  • 19
0

I'm not sure it will be helpful or not. As far as I get from your question that you want EmployeeName to be distinct irrelevant to other columns. But if you do ToTable and turn on the distinct flag it will give all the distinct rows, doesn't matter how many columns are involved there. So if you mention only EmployeeName it will obviously give you distinct EmployeeNames, not all the columns associated with it.

So, thats what I did, initially select only the distinct EmployeeName columns and put it into a temp DataTable dtt.

DataTable dtt = dvFail.DefaultView.ToTable(true, "EmployeeName");

Secondly I've created another temp DataTable where we put the segregated rows from the main DataTable dtFail and set the column names manually.

DataTable TempDataTable = new DataTable();
DataTable dtFailed = new DataTable();

Prepare the columns in the dtFailed DataTable.

     if (dtFailed.Columns.Count == 0)
     {
           dtFailed.Columns.Add("EmployeeName");
           dtFailed.Columns.Add("EmployeeRole");
           dtFailed.Columns.Add("Status");
           dtFailed.Columns.Add("Date");
     }

Loop through the distinct EmployeeName dtt DataTable and match the EmployeeName and keep that selected first row in the TempDataTable. Finally all rows transferred into the dtFailed.

     for (int j = 0; j < dtt.Rows.Count; j++)
     {
           string EmployeeName = dtt.Rows[j]["EmployeeName"].ToString();
           TempDataTable = dvFail.Select("EmployeeName = " + EmployeeName).CopyToDataTable();
           dtFailed.Rows.Add(TempDataTable.Rows[0].ItemArray);
     }
Mahib
  • 3,977
  • 5
  • 53
  • 62