View Full Version : Select Distinct From DataTable?
christrinder
04-28-2004, 09:16 AM
I'm bringing back from the DB at DataSet of information, one column of which includes a type. Is there anyway I can do a sort of "select distinct" on this type so that I can populate a dropdown with the relevant types? Obviously I could build a table by looping through the orgininal dataset and for each record I could then loop through the new table and compare the value. This way I would only be adding into the new datatable those that don't already appear in there, but this seems a bit long winded. Is there a better way? P.S. I'm coding in ASP.NET C#. Thanks, Chris
Roelf
04-28-2004, 11:42 AM
Are you connecting the whole table to the dataset? In that case, why dont you use the db-query (SELECT DISTINCT col1, col2,col2 FROM table) to generate a recordset, and connect that to the dataset?
christrinder
04-28-2004, 12:25 PM
Yes, the DataSet is used to fill a datagrid where I need to display all records, but I want to use a dropdown above it which includes the distinct types (one of the columns) within that datagrid. Any ideas?
Roy Sinclair
04-28-2004, 04:49 PM
"select distinct columnname from tablename" should do the trick.
christrinder
04-28-2004, 08:20 PM
That syntax is fine on the server, but I'm trying to select distinct records from a DataTable on the codebehind. Any other ideas?
mrajpurkar
04-24-2006, 07:33 PM
U can try this. It should work.
so u can call like this.
object[] distinctRoomType = GetDistinctValues(dt,"Roomtype");
Here is the method definition.
public object[] GetDistinctValues(DataTable dtable,string colName)
{
Hashtable hTable = new Hashtable();
foreach(DataRow drow in dtable.Rows)
{
try
{
hTable.Add(drow[colName],string.Empty);
}
catch{}
}
object[] objArray = new object[hTable.Keys.Count ];
hTable.Keys.CopyTo(objArray,0);
return objArray;
}
let me know if it helped you...
Baleric
04-25-2006, 03:38 AM
you know that ws posted 2 years ago lol.
christrinder
04-25-2006, 09:45 AM
Hey, thanks for replying anyway! Can't remember how I solved it in the end.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.