datatable.select("Distinct") or such to string?

G

Guest

I am hitting a bit of a wall - I am building a table -

public DataTable theTownships = new DataTable("AdjacentTownships");
public DataTable buildTownshipTable()
{
DataColumn tscolumn; //Townships-Range Field
tscolumn = new DataColumn();
tscolumn.DataType = System.Type.GetType("System.String");
tscolumn.ColumnName = "TSR";
tscolumn.ReadOnly = false;
tscolumn.Unique = false;
theTownships.Columns.Add(tscolumn);

return theTownships;
}

This table is built from another query where each coloumn in that row is
turned into a row here. So I can have between 1 and 0 rows, some can be
duplicates.

What I need to do is then select the distinct rows from this table and pass
them to a for loop that I can then parse/process each of the distinct rows.

Any and all suggestions would be greatly appreciated!!
 
G

Guest

You might be interested in the assembly I've been working on at
http://www.queryadataset.com. Besides DISTINCT, it lets you perform complex
SQL SELECT statements including UNION, JOINS, GROUP BY, HAVING, ORDER BY,
sub-queries, etc against the tables in a dataset.

The web-site allows you to upload your own XML data fragment, DataSet or
resultset and issue queries using the QueryADataSet assembly.

Adrian Moore
http://www.queryadataset.com
 
G

Guest

Ok, that looks interesting, but how can I use it against a table I have
created in memory. Then put the data that is returned into a format that I
can feed into my for loop?
 
A

Adrian Moore

Just to clarify my understanding. You have a DataTable called
AdjacentTownships that contains a column TSR and rows which contain
duplicate data in the TSR column. You want to retrieve a distinct set of
values for the TSR column and process each row in a for loop.

Make sure the DataTable belongs to a DataSet, then using the QueryADataSet
assembly,

DataView dv = QueryADataSet.DsCommand.Execute("SELECT DISTINCT TSR FROM
AdjacentTownships" , ds);

for (int i = 0; i < dv.Count; i++)
{
DataRow dr = dv.Row;
// parse/process data in row
}

If you want distcint rows, then the query becomes SELECT DISTINCT * FROM
AdjacentTownships

Hope this helps
Ad.

"(e-mail address removed)"
 
S

Steven Cheng[MSFT]

Hi Dewright,

For datatable retrieveing from physical DATABASE, we'll recommend doing the
filtering in the database's SQL query. For your scenario, since the
DataTable are maually created in memory on the fly, I think we may need to
do the filtering through a loop ourselve since the DataTable's buildin
select method didn't provide Distince function. Here are some articles
which discussing on doing distinct selection on .NET's DataTable manually:

#Select DISTINCT on DataTable
http://weblogs.asp.net/eporter/archive/2005/02/10/370548.aspx

#HOW TO: Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
http://support.microsoft.com/?id=326176


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

These both look great, will give them a shot in the morning!

Once again I think you have saved my world Steven!
 
G

Guest

Steven,
I went through the MSDN Article and got the code mostly implemented.

dsHelper.SelectDistinct("thinTownships", ds.Tables["AdjacentTownships"],
"TSR");

When I try to assign my datagrid like this,
DataGrid2.SetDataBinding(ds, "thinTownships");

I get a message about WebControl.Datagrid does not contain a definition for
SetDataBinding. So I tried this,

DataGrid2.DataSource = "thinTownships";
DataGrid2.DataBind();

This code returns a table that just has the column header and it actually
put the string thinTownships vertically broken up by charecters.

Any thoughts on how I might be able to do this using a web grid?
 
G

Guest

Never you mind, I figured out how to do it, just needed to think my way
through!

Thanks again works like a charm!
--
D @ premierdata


Steven,
I went through the MSDN Article and got the code mostly implemented.

dsHelper.SelectDistinct("thinTownships", ds.Tables["AdjacentTownships"],
"TSR");

When I try to assign my datagrid like this,
DataGrid2.SetDataBinding(ds, "thinTownships");

I get a message about WebControl.Datagrid does not contain a definition for
SetDataBinding. So I tried this,

DataGrid2.DataSource = "thinTownships";
DataGrid2.DataBind();

This code returns a table that just has the column header and it actually
put the string thinTownships vertically broken up by charecters.

Any thoughts on how I might be able to do this using a web grid?
--
D @ premierdata


Steven Cheng said:
You're welcome Dewright :)

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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