How do I select distinct rows from a DataTable?

  • Thread starter Thread starter Jim H
  • Start date Start date
J

Jim H

I am storing incoming data in memory using a DataTable. After I'm done
retrieving the data I need to get the distinct rows. I tried using
DataTable.Select but that doesn't work.

If I have columns "col1", "col2", "col3" in my DataTable "Table1", I need to
basically be able to do this:
select distinct col2 form Table1

I tried doing this:
DataRow[] ResultsRowArray = MyTable.Select("distinct col2");

This obviously does not work. How can I get back an array of distinct rows?
I don't care if it all 3 columns or just that one.

BTW I am not connected to any databases. I don't need to store the data
like that permanently.

Thanks,
jim
 
Jim H said:
I am storing incoming data in memory using a DataTable. After I'm done
retrieving the data I need to get the distinct rows. I tried using
DataTable.Select but that doesn't work.

If I have columns "col1", "col2", "col3" in my DataTable "Table1", I need to
basically be able to do this:
select distinct col2 form Table1

I tried doing this:
DataRow[] ResultsRowArray = MyTable.Select("distinct col2");

I don't believe the DataTable Select method supports the DISTINCT keyword in any current version. It appears you may have to code it yourself.

The following Microsoft link has another link to something called the SelectDistinct method. You could use this in your own derived DataTable class or just add it to on of your existing assemblies.

http://support.microsoft.com/default.aspx?scid=kb;en-us;326176#1

- carl
 
That's what I thought.
Thanks,
jim

Jim H said:
I am storing incoming data in memory using a DataTable. After I'm done
retrieving the data I need to get the distinct rows. I tried using
DataTable.Select but that doesn't work.

If I have columns "col1", "col2", "col3" in my DataTable "Table1", I need to
basically be able to do this:
select distinct col2 form Table1

I tried doing this:
DataRow[] ResultsRowArray = MyTable.Select("distinct col2");

I don't believe the DataTable Select method supports the DISTINCT keyword in
any current version. It appears you may have to code it yourself.

The following Microsoft link has another link to something called the
SelectDistinct method. You could use this in your own derived DataTable
class or just add it to on of your existing assemblies.

http://support.microsoft.com/default.aspx?scid=kb;en-us;326176#1

- carl
 
Back
Top