How to select distinct values from datatable in C#?
For a retrieved data from database, I need to get its distinct value in C#?
How to select distinct values from datatable in C#?
For a retrieved data from database, I need to get its distinct value in C#?
If by "in C#" you mean using LINQ then you can use the Distinct Operator.
sthing like ?
SELECT DISTINCT .... FROM table WHERE condition
http://www.felixgers.de/teaching/sql/sql_distinct.html
note: Homework question ? and god bless google..
DataTable dt = new DataTable();
dt.Columns.Add("IntValue", typeof(int));
dt.Columns.Add("StringValue", typeof(string));
dt.Rows.Add(1, "1");
dt.Rows.Add(1, "1");
dt.Rows.Add(1, "1");
dt.Rows.Add(2, "2");
dt.Rows.Add(2, "2");
var x = (from r in dt.AsEnumerable()
select r["IntValue"]).Distinct().ToList();
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2" ...);
To improve the above answer: The ToTable function on dataview has a "distinct" flag.
//This will filter all records to be distinct
dt = dt.DefaultView.ToTable(true);