Blog

How to get Disticnt rows from a DataSet or Datatable?

Posted by Shyju Blogs | Current Affairs May 25, 2007
In many situation we need to avoid duplicate rows from data table. This will help to solve the issue.

How can I Retrieve distinct rows? -- How can I avoid duplicate rows from a DataTable?

One of the common issues in data layer is avoiding duplicate rows from dataset or datatable. I saw many people are writing separate function and looping through the datatable to avoid the duplicates. There is more simple ways available in .Net but people are unaware about this. I thought of writing a blog about this because I saw many blogs which mislead the people from right path. Thers is no need of looping or no need of logic are required to avoid the duplicates.

Following single line of code will avoid the duplicate rows.

ds.Tables["Employee"].DefaultView.ToTable(true,"employeeid");

ds - Dataset object

dt.DefaultView.ToTable( true, "employeeid");

dt - DataTable object

First option in ToTable is a boolean which indicates, you want distinct rows or not?

Second option in the ToTable is the column name based on which we have to select distinct rows.

Simple right, this option is the last one so most of the people didn't got the time to find it. Now your code will look much cleaner without those junk codes.

COMMENT USING
PREMIUM SPONSORS
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
Employers - Post Free Jobs
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
Employers - Post Free Jobs