Summary of methods for traversing various data sets in C

  • 2020-06-12 08:49:15
  • OfStack

In C#, the methods of traversing various data sets are summarized as follows:
1. Enumerated types
 
// Traversal enumerated types Sample Is the name of each enumeration  
foreach (string sp in Enum.GetNames(typeof(Sample))) 
{ 
ary.Add(sp); 
} 
// Traversal enumerated types Sample Each enumeration value of  
foreach (string sp in Enum.GetValues(typeof(Sample))) 
{ 
ary.Add(sp); 
} 

2. Traversal ArrayList(Queue, Stack)
Take string as an example. Of course, elements in ArrayList can be of any data type, and the traversal needs to ensure that elements in ArrayList are of the same data type.
 
// The traversal element is string Type of queue  
foreach (string text in arraylist) 
{ 
ary.Add(text); 
} 

In addition, Queue queue and Stack stack are traversed in the same way as ArrayList. foreach can be used to loop through them, except that one is fifo and the other is fifo.
3. Controls in Winform Forms
 
// Traverse to find controls in the main form and remove qualifying controls from the form  
foreach (Control ctl in this.Controls) 
{ 
// Gets and determines the control type or control name  
if (ctl.GetType().Name.Equals("ListBox") || ctl.Name.Equals("listBox1")) 
this.Controls.Remove(ctl); 
} 

4. HashTable hash table
The DictionaryEntry class needs to refer to ES27en.Collections
 
// Iterate through the keys and values in the complete hash table  
foreach (DictionaryEntry item in hashTable) 
{ 
ary.Add(" Hash keys: "+item.Key+", The hash value: "+item.Value.ToString()); 
} 
 You can also iterate over the keys or values in the hash table separately.  
// Only the keys in the hash table are traversed  
foreach (string key in hashTable.Keys) 
{ 
ary.Add(" Hash keys: " + key); 
} 
// Only the values in the hash table are traversed  
foreach (string value in hashTable.Values) 
{ 
ary.Add(" The hash value: " + value); 
} 

5. Iterate over the rows and columns in DataSet and DataTable
 
// traverse DataSet In the table  
foreach (DataTable dt in dataSet.Tables) 
{ 
ary.Add(" The name of the table: " + dt.TableName.ToString()); 
} 
// traverse DataSet The default first 1 Rows in a table  
foreach (DataRow dr in dataSet.Tables[0].Rows) 
{ 
// Gets data for a field (column) in a row  
ary.Add(dr["ID"].ToString()); 
} 
// traverse DataSet The default first 1 Columns in a table  
foreach (DataColumn col in dataSet.Tables[0].Columns) 
{ 
ary.Add(" The column name: "+col.ColumnName); 
} 

DataTable traverses rows and columns in a similar way to DataSet, except that dataSet.Tables [0] is replaced with a specific table.
You can also perform SQL queries on DataTable tables, and then iterate over the query results.
 
// traverse DataSet Where the SELECT The result of executing the query condition  
foreach (DataRow dr in dataSet.Tables[0].Select(" MONTH>6 AND MONTH<12 ")) 
{ 
// Gets data for a field (column) in a row  
ary.Add(dr["ID"].ToString()); 
} 

6. Iterate over the rows in DataGridView
 
// traverse DataGridView The lines in the  
foreach (DataGridViewRow dr in dataGridView1.Rows) 
{ 
// Gets data for a field (column) in a row  
ary.Add(dr.Cells["ID"].ToString()); 
} 

7. Iterate through item in ListBOX and ComboBox
1 Generally, foreach traversal can only be traversed to the name of item in ListBOX and ComboBox. The item data that needs to be added when binding item for the complete traversal is an object of the custom class with 2 attributes. The name of one attribute in the object is regarded as DisplayMember(item name) and the other as DisplayValue(item value). This will get the name and value of item in ListBOX and ComboBox as you traverse.

Related articles: