Home > Article > Backend Development > Summary of methods for traversing various data collections in C#
How to traverse various data collections in C#, here is a summary:
1. Enumeration type
//遍历枚举类型Sample的各个枚举名称 foreach (string sp in Enum.GetNames(typeof(Sample))) { ary.Add(sp); } //遍历枚举类型Sample的各个枚举值 foreach (string sp in Enum.GetValues(typeof(Sample))) { ary.Add(sp); }
2. Traverse ArrayList (Queue, Stack)
Here string is For example, of course, the elements in the ArrayList can be of any data type. When traversing, you must confirm that the elements in the ArrayList are all of the same data type.
//遍历元素为string类型的队列 foreach (string text in arraylist) { ary.Add(text); }
In addition, the way to traverse the Queue queue and Stack stack is basically the same as ArrayList. You can use foreach to loop through, except that one is first in, first out and the other is first in, last out.
3.Controls in Winform form
//遍历寻找主窗体中的控件,并将符合条件的控件从窗体上去除 foreach (Control ctl in this.Controls) { //获取并判断控件类型或控件名称 if (ctl.GetType().Name.Equals("ListBox") || ctl.Name.Equals("listBox1")) this.Controls.Remove(ctl); }
4.HashTable hash table
DictionaryEntry class needs to reference System.Collections
//遍历完整哈希表中的键和值 foreach (DictionaryEntry item in hashTable) { ary.Add("哈希键:"+item.Key+",哈希值:"+item.Value.ToString()); } 此外还可以单独遍历哈希表中的键或值。 //只遍历哈希表中的键 foreach (string key in hashTable.Keys) { ary.Add("哈希键:" + key); } //只遍历哈希表中的值 foreach (string value in hashTable.Values) { ary.Add("哈希值:" + value); }
5. Traverse rows in DataSet and DataTable and columns
//遍历DataSet中的表 foreach (DataTable dt in dataSet.Tables) { ary.Add("表名:" + dt.TableName.ToString()); } //遍历DataSet中默认第一个表中的行 foreach (DataRow dr in dataSet.Tables[0].Rows) { //获取行中某个字段(列)的数据 ary.Add(dr["ID"].ToString()); } //遍历DataSet中默认第一个表中的列 foreach (DataColumn col in dataSet.Tables[0].Columns) { ary.Add("列名:"+col.ColumnName); }
The method of traversing rows and columns in DataTable is similar to that of DataSet, except that dataSet.Tables[0] is replaced with a specific table.
In addition, you can also perform SQL queries on the DataTable table, and then traverse the query results.
//遍历DataSet中表SELECT执行查询条件后的结果 foreach (DataRow dr in dataSet.Tables[0].Select(" MONTH>6 AND MONTH<12 ")) { //获取行中某个字段(列)的数据 ary.Add(dr["ID"].ToString()); }
6. Traverse the rows in the DataGridView
//遍历DataGridView中的行 foreach (DataGridViewRow dr in dataGridView1.Rows) { //获取行中某个字段(列)的数据 ary.Add(dr.Cells["ID"].ToString()); }
7. Traverse the items in ListBOX and ComboBox
Generally, foreach traversal can only traverse the names of items in ListBOX and ComboBox , The item data that needs to be added when binding the item for a complete traversal is an object of a binary attribute custom class, and the name of one attribute in the object is used as DisplayMember (item name), and the other is used as DisplayValue (item value). In this way, all the names and values of the items in ListBOX and ComboBox can be obtained during traversal.
The above is a summary of the methods of traversing various data collections in C#. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!