select from dataset

  • Thread starter justanothernewbie804
  • Start date
J

justanothernewbie804

hi all. I have a dataset and would like to do something like "select
distinct field1, field2 from fields" against the dataset. I do not
have the option of creating a new database table and selecting from
that table. Nor do I have the option of changing how the dataset is
populated. I need to select distinct from the dataset that I have.
Any suggestions?
 
A

Alberto Poblacion

hi all. I have a dataset and would like to do something like "select
distinct field1, field2 from fields" against the dataset. I do not
have the option of creating a new database table and selecting from
that table. Nor do I have the option of changing how the dataset is
populated. I need to select distinct from the dataset that I have.
Any suggestions?

Unfortunately, the Select() in the DataTable does not provide the
Distinct operator. You will have to filter the rows with a loop in your own
code. One way to do it is to first sort the data on the fields that need to
be distinct (for instance, using Sort by means of an interposed DataView),
and then iterate over all records with a for loop, extracting only those
records whose fields do not match those of the record processed in the
previous iteration.
 
J

justanothernewbie804

Unfortunately, the Select() in the DataTable does not provide the
Distinct operator. You will have to filter the rows with a loop in your own
code. One way to do it is to first sort the data on the fields that need to
be distinct (for instance, using Sort by means of an interposed DataView),
and then iterate over all records with a for loop, extracting only those
records whose fields do not match those of the record processed in the
previous iteration.

thanks, that's what I suspected, but was hoping for a more sql like
capabilty.
 
N

Nicholas Paldino [.NET/C# MVP]

I don't know (highly doubt) that this will help you now, but with C# 3.0
and .NET 3.5, you will be able to do this (both in beta right now). It will
allow you to do something like this:

// Your data table is in dt
IEnumerable<DataRow> rows = dt.AsEnumerable().Distinct();

Then, you will get just distinct rows from the datatable.

If you need to load it back into another datatable for use, you can do
this:

// This can be typed as well. If so, use the overload which takes a
datatable which has
// a matching structure.
DataTable newDt = dt.AsEnumerable().Distinct().CopyToDataTable();

Hope this helps.
 
I

Ivica Muruzovic

Look in Microsoft visual studio 2005 documention ToTable method for
DataTable
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top