illustrate:
DataTable filtering, some commonly used methods are: Select, dataview
1. Direct loop traversal to obtain
// Assume dt is the result of "SELECT C1, C2, C3 FROM T1"DataTable dt = new DataTable(); for (int i = 0; i < ; i++) { if ([i]["C1"].ToString() == "abc")//Query conditions { //Perform operation } }
2. Use LinQ
// (Conditions [basically similar to Db's Sql filtering])DataTable dt = new DataTable(); DataRow[] drArr = (" C1='abc' "); //Inquiry//( "ID=" + id ) //Multiple conditions, such as: ("ID=" + id +" and name=" +name) // Fuzzy filteringDataRow[] drArr = ("C1 LIKE 'abc%'"); // Another method of fuzzy queryDataRow[] drArr = ("'abc' LIKE C1 + '%'", "C2 DESC"); // Filter and sortDataRow[] drArr = ("C1='abc'", "C2 DESC"); // How to convert DataRow to DataTable?// Idea: DataRow assigns value to a new DataTableDataTable dtNew = (); for (int i = 0; i < ; i++) { (drArr[i]); }
3. Use DataView's RowFilter to achieve filtering
DataTable dataSource = new DataTable(); DataView dv = ; = "columnA = 'abc'"; //1. After filtering, directly obtain DataTableDataTable newTable1 = (); //2. Set the TableName of the new DataTableDataTable newTable2 = ("NewTableName"); //3. Set whether the new table filters duplicates, the column names of the owned columns and the order in which they appear//The fields of the new table can be set. But the field name must be owned by the old cousin dataSource.DataTable newTable3 = (true, new string[] { "columnA,columnF,columnC" }); //4. Comprehensive 2.3 points.DataTable newTable4 = ("NewTableName", true, new string[] { "columnA,columnF,columnC" });
This is the end of this article about the example of DataTable filtering query implementation in C#. For more related C# DataTable filtering query content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!