Search code examples
c#.netvb.netsql-server-2008-r2strongly-typed-dataset

How to get distinct values from a datatable in strongly typed dataset


I am using strongly typed dataset and have many tables in that..

The problem is now i want to filter data from

GetData()

function which has a query like

select * from table_name

How can i filter a particular table and distinct values from it. Also if i try to filter it return all column but rest have null values except the one i asked, so i cannot assign it as a datasource to a datagrid or combobox

How can i do this..


Solution

  • How about the Select() method of the DataTable?

    DataRow[] filtered = someDataSet.SomeDataTable.Select("Status = 'Active'");
    

    Edit:
    Updated code sample following OP's comment

    using System.Linq;
    ...
    DataRow[] rows = someDataSet.SomeDataTable.Select("Status = 'Active'");
    string[] columnValues = row.Select(x => x["SomeColumnName"].ToString());
    

    Note, the two Select() methods are different. The first is a DataTable method that filters rows. The second is a linq extension method that is transforming an array of rows into an array of strings.