ASP. NET DataTable remove duplicate rows
This article mainly introduces two methods for removing duplicate rows from ASP. NET DataTable. This article will directly remove duplicated rows. For more information, see
First, use the Linq query expression. The code is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
DataTable testtable = new DataTable (); Testtable. Columns. Add ("ID "); Testtable. Columns. Add ("ProductName "); Testtable. Rows. Add ("1", "1 "); Testtable. Rows. Add ("1", "1 "); Testtable. Rows. Add ("1", "1 "); Testtable. Rows. Add ("2", "2 "); Testtable. Rows. Add ("3", "3 "); DataTable finalltable = new DataTable (); Finalltable = testtable. Clone (); Finalltable. Clear (); Var rows = from row in testtable. AsEnumerable () group row by row ["ID"] into myrow select myrow. FirstOrDefault (); Foreach (DataRow row in rows) { Finalltable. ImportRow (row ); } |
Method 2
Use dataview to filter datatable
?
1 2 |
<STRONG> testtable = testtable. DefaultView. ToTable (true, new string [] {"ID", "ProductName "}); </STRONG> |