C # method summary for Traversing various data sets,

Source: Internet
Author: User

C # method summary for Traversing various data sets,

Summary of the methods for Traversing various data sets in C:
1. Enumeration type 

// Print the names of samples of the enumeration type foreach (string sp in Enum. getNames (typeof (Sample) {ary. add (sp);} // traverse the enumerated values foreach (string sp in Enum. getValues (typeof (Sample) {ary. add (sp );}

2. Traverse ArrayList (Queue, Stack) 
Here we take string as an example. Of course, the elements in the ArrayList can be of any data type, and the time duration must be confirmed that the elements in the ArrayList are of the same data type.

// Print queues whose elements are string type foreach (string text in arraylist) {ary. Add (text );}

In addition, the way to traverse Queue queues and Stack Stacks is basically the same as that of ArrayList. You can use foreach to traverse the queues cyclically, but one is first-in-first-out and the other is first-out.

ArrayList list = new ArrayList (); // for traversing for (int I = 0; I <list. count; I ++) {SE se SE = (SE) list [I]; Console. writeLine (se. name);} // foreach traverses foreach (Object obj in list) {SE se = (SE) list [I]; Console. writeLine (se. name );}


3. Controls in Winform 

// Retrieve the Control in the main form and remove the Control in this. controls) {// obtain and determine 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 must reference System. Collections.

// Traverse the keys and values in the complete hash table foreach (DictionaryEntry item in hashTable) {ary. add ("hash key:" + item. key + ", hash value:" + item. value. toString ();} You can also traverse the keys or values in the hash table separately. // Just traverse the key foreach (string key in hashTable. keys) {ary. add ("hash key:" + key);} // only the value foreach (string value in hashTable. values) {ary. add ("hash value:" + value );}

5. Traverse rows and columns in DataSet and DataTable 

// Traverse the table foreach (DataTable dt in DataSet. tables) {ary. add ("table name:" + dt. tableName. toString ();} // traverse the row foreach (DataRow dr in DataSet. tables [0]. rows) {// obtain the data ary of a field (column) in the row. add (dr ["ID"]. toString ();} // traverse the default foreach (DataColumn col in DataSet) column in the first table in dataSet. tables [0]. columns) {ary. add ("column name:" + col. columnName );}

The method for able to traverse rows and columns is similar to that for DataSet. Just replace dataSet. Tables [0] with a specific table.
In addition, you can perform SQL queries on the able table and then traverse the query results.

// Print the result foreach (DataRow dr in DataSet. tables [0]. select ("MONTH> 6 and month <12") {// obtain the data ary of a field (column) in the row. add (dr ["ID"]. toString ());}

6. traverse the rows in the DataGridView. 

// Traverse the row foreach (DataGridViewRow dr in dataGridView1.Rows) in THE DataGridView {// obtain the data ary of a field (column) in the row. add (dr. cells ["ID"]. toString ());}

7. Traverse items in ListBOX and ComboBox 
Generally, foreach traversal can only traverse the names of items in ListBOX and ComboBox. The full traversal requires that the item data added when the item is bound is a binary property custom class object, use the name of one attribute of the object as DisplayMember (item name), and the other as DisplayValue (item value ). In this way, you can obtain the names and values of items in ListBOX and ComboBox during traversal.

8. List <T>

// For traversal for (int I = 0; I <list. count; I ++) {// you do not need to convert the data type to Console. writeLine (list [I]);} // foreach traverses foreach (SE obj in list) {// The type conversion Console is not required for the duration. writeLine (obj );}

9. Dictionary <K, V>

// Traverse Valuesforeach (SE se in list. values) {// you do not need to convert the data type to Console for the time duration. writeLine (se);} // traverse foreach (KeyValuePair <string, SE> en in list) {Console. writeLine (en. key); Console. writeLine (en. value. name);} // KeyValuePair <TKey, TValue> is a generic structure

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.