views:

56

answers:

4

I have a dataset that gets populated from a stored proc in sql server. I have a column that has lets say has a set of values. I do not know what those values are. All I know is that they of the type "string". I want to extract all the distinct values from that column.

A: 

You can simply use Select method of the DataTable :

DataRow[] extractedRows = 
     yourDataSet.Tables["YourTableName"].Select("YourColumnName = 123");
Canavar
I am sorry. I have to rephrase my question. My bad. I have a column that has lets say a set of value. I do not know what those values are. All I know is that they of the type string. I want to extract all the distinct value from that column. I hope that it is clear now.
VP
A: 

You can also use Linq to query your datatables. Here is a link to some examples on the MS site - http://msdn.microsoft.com/en-us/vbasic/bb688086.aspx

czuroski
+1  A: 

You can use a DataView and set its RowFilter to the desired condition:

var view = new DataView(dataset.Tables["Table"]);
view.RowFilter = "Column = 42";

UPDATE: based on your updated question, you can use LINQ:

var table = dataset.Tables["Table"].AsEnumerable();
var distinctValuesForColumn = 
  table.Select(row => (string)row["Column"]).Distinct();
Jordão
I am sorry. I have to rephrase my question. My bad. I have a column that has lets say a set of value. I do not know what those values are. All I know is that they of the type string. I want to extract all the distinct value from that column. I hope that it is clear now.
VP
Please update the question itself instead of giving this kind of information in a comment.
Jordão
Thanks for the idea Jordao. However it throws me an error. It does not recongnize the Distinct(). Do I have to change the variable type from var to something for this to work?
VP
I am using exactly what you typed except that I am using dataset.Tables[0] instead of Tables["table name"] as my dataset contains only 1 table.
VP
[Distinct](http://msdn.microsoft.com/en-us/library/system.linq.enumerable.distinct.aspx) is part of LINQ, which debuted in the .NET Framework 3.5. If `Select` works, it's strange that `Distinct` doesn't. See if you are `using System.Linq;`, and which version of the framework you're targeting. Did you also use the `AsEnumerable` call?
Jordão
The returned value will have the type `IEnumerable<string>`, but you can still use `var` to declare it.
Jordão
wow..works perfect now. Just like you said I was missing using System.Linq assembly. Thanks a ton for this. I am however not able to access individual rows like I used to in a dataset. I mean I want to now see what distinct values the enumeration contains. How do I see that?
VP
I plan to use a foreach (string str in varnameoftypeIenumerable). Is there a better way to index contents inside an IWNUMERABLE?
VP
You can still use the `DataSet` for that.
Jordão
A: 

I hope below statement will serve your purpose

ds.Tables["TableName"].DefaultView.ToTable( true, "columnName"); //For Dataset (true means distinct)

OR

   `ds.Tables[0].DefaultView.ToTable( true, "columnName");

//For Dataset where tableindex is 0

OR

dt.DefaultView.ToTable( true, "columnName"); //For Datatable

//Syntax is like Datatable.DefaultView.ToTable( Distinct true/false, “ColumnName”);

MSDN : http://msdn.microsoft.com/en-us/library/wec2b2e6.aspx

Aamod Thakur