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
2010-09-28 18:06:20
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
2010-09-28 18:16:33
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
2010-09-28 18:09:09
+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
2010-09-28 18:10:10
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
2010-09-28 18:18:33
Please update the question itself instead of giving this kind of information in a comment.
Jordão
2010-09-28 18:22:52
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
2010-09-28 18:28:39
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
2010-09-28 18:32:27
[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
2010-09-28 18:33:35
The returned value will have the type `IEnumerable<string>`, but you can still use `var` to declare it.
Jordão
2010-09-28 18:41:32
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
2010-09-28 18:44:36
I plan to use a foreach (string str in varnameoftypeIenumerable). Is there a better way to index contents inside an IWNUMERABLE?
VP
2010-09-28 18:48:28
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
2010-09-28 18:36:49