C # DataTable duplicate data efficiency filter method
Use the DataView, then set Totable, several fields and setting a Boolean value, which represents the field as a whole can not be duplicated in this table , the sample code:
namespace A { class Program { static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add("Id",typeof(int)); Random r = new Random(DateTime.Now.Millisecond); // 1000 rows randomly generated for (int i = 0; i < 1001; i++) { dt.Rows.Add(r.Next(1, 11)); } DataView dv = new DataView(dt); dt = dv.ToTable (true, "Id"); // for filter Id, true indication distinct methods foreach (DataRow item in dt.Rows) { Console.WriteLine(item["Id"].ToString()); } } } }
The above datatable only one, if it is multi-column, then go heavy follows
DataView dv = new DataView(dt); dt = dv.ToTable(true, new string[] { "Id","Value" });