DataTable轉換成IList

 本文轉載自http://blog.csdn.net/chentaihan/article/details/6407284數據庫

 做者:陳太漢ide

    在用C#做開發的時候常常要把DataTable轉換成IList;操做DataTable比較麻煩,把DataTable轉換成IList,以對象實體做爲IList的元素,操做起來就很是方便。編碼

注意:實體的屬性必須和數據庫中的字段必須一一對應,或者數據庫字段名.ToLower().Contains(實體屬性名.ToLower())spa

          數據類型暫時只支持int、string、DateTime、float、double.net

 

 

  
  
  
  
  1. using System; 
  2. using System.Collections; 
  3. using System.Collections.Generic; 
  4. using System.Data; 
  5. using System.Reflection; 
  6. namespace TBToListTest 
  7.     public class TBToList<T> where T : new() 
  8.     { 
  9.         /// <summary> 
  10.         /// 獲取列名集合 
  11.         /// </summary> 
  12.         private IList<string> GetColumnNames(DataColumnCollection dcc) 
  13.         { 
  14.             IList<string> list = new List<string>(); 
  15.             foreach (DataColumn dc in dcc) 
  16.             { 
  17.                 list.Add(dc.ColumnName); 
  18.             } 
  19.             return list; 
  20.         } 
  21.         /// <summary> 
  22.         ///屬性名稱和類型名的鍵值對集合 
  23.         /// </summary> 
  24.         private Hashtable GetColumnType(DataColumnCollection dcc) 
  25.         { 
  26.             if (dcc == null || dcc.Count == 0) 
  27.             { 
  28.                 return null
  29.             } 
  30.             IList<string> colNameList = GetColumnNames(dcc); 
  31.             Type t = typeof(T); 
  32.             PropertyInfo[] properties = t.GetProperties(); 
  33.             Hashtable hashtable = new Hashtable(); 
  34.             int i = 0; 
  35.             foreach (PropertyInfo p in properties) 
  36.             { 
  37.                 foreach (string col in colNameList) 
  38.                 { 
  39.                     if (col.ToLower().Contains(p.Name.ToLower())) 
  40.                     { 
  41.                         hashtable.Add(col, p.PropertyType.ToString() + i++); 
  42.                     } 
  43.                 } 
  44.             } 
  45.             return hashtable; 
  46.         } 
  47.         /// <summary> 
  48.         /// DataTable轉換成IList 
  49.         /// </summary> 
  50.         /// <param name="dt"></param> 
  51.         /// <returns></returns> 
  52.         public IList<T> ToList(DataTable dt) 
  53.         { 
  54.             if (dt == null || dt.Rows.Count == 0) 
  55.             { 
  56.                 return null
  57.             } 
  58.             PropertyInfo[] properties = typeof(T).GetProperties();//獲取實體類型的屬性集合 
  59.             Hashtable hh = GetColumnType(dt.Columns);//屬性名稱和類型名的鍵值對集合 
  60.             IList<string> colNames = GetColumnNames(hh);//按照屬性順序的列名集合 
  61.             List<T> list = new List<T>(); 
  62.             T model = default(T); 
  63.             foreach (DataRow dr in dt.Rows) 
  64.             { 
  65.                 model = new T();//建立實體 
  66.                 int i = 0; 
  67.                 foreach (PropertyInfo p in properties) 
  68.                 { 
  69.                     if (p.PropertyType == typeof(string)) 
  70.                     { 
  71.                         p.SetValue(model, dr[colNames[i++]], null); 
  72.                     } 
  73.                     else if (p.PropertyType == typeof(int)) 
  74.                     { 
  75.                         p.SetValue(model, int.Parse(dr[colNames[i++]].ToString()), null); 
  76.                     } 
  77.                     else if (p.PropertyType == typeof(DateTime)) 
  78.                     { 
  79.                         p.SetValue(model, DateTime.Parse(dr[colNames[i++]].ToString()), null); 
  80.                     } 
  81.                     else if (p.PropertyType == typeof(float)) 
  82.                     { 
  83.                         p.SetValue(model, float.Parse(dr[colNames[i++]].ToString()), null); 
  84.                     } 
  85.                     else if (p.PropertyType == typeof(double)) 
  86.                     { 
  87.                         p.SetValue(model, double.Parse(dr[colNames[i++]].ToString()), null); 
  88.                     } 
  89.                 } 
  90.                 list.Add(model); 
  91.             } 
  92.             return list; 
  93.         } 
  94.         /// <summary> 
  95.         /// 按照屬性順序的列名集合 
  96.         /// </summary> 
  97.         private IList<string> GetColumnNames(Hashtable hh) 
  98.         { 
  99.             PropertyInfo[] properties = typeof(T).GetProperties();//獲取實體類型的屬性集合 
  100.             IList<string> ilist = new List<string>(); 
  101.             int i = 0; 
  102.             foreach (PropertyInfo p in properties) 
  103.             { 
  104.                 ilist.Add(GetKey(p.PropertyType.ToString() + i++, hh)); 
  105.             } 
  106.             return ilist; 
  107.         } 
  108.         /// <summary> 
  109.         /// 根據Value查找Key 
  110.         /// </summary> 
  111.         private string GetKey(string val, Hashtable tb) 
  112.         { 
  113.             foreach (DictionaryEntry de in tb) 
  114.             { 
  115.                 if (de.Value.ToString() == val) 
  116.                 { 
  117.                     return de.Key.ToString(); 
  118.                 } 
  119.             } 
  120.             return null
  121.         } 
  122.            
  123.     } 
  124.  
  125. namespace TBToListTest 
  126.    //實體 
  127.     public class Person 
  128.     { 
  129.         public int ID 
  130.         { 
  131.             set
  132.             get
  133.         } 
  134.         public string Name 
  135.         { 
  136.             set
  137.             get
  138.         } 
  139.         public string Age 
  140.         { 
  141.             set
  142.             get
  143.         } 
  144.         public string Lover 
  145.         { 
  146.             set
  147.             get
  148.         } 
  149.     } 
  150. using System; 
  151. using System.Data; 
  152. namespace TBToListTest 
  153.     class Program 
  154.     { 
  155.         static void Main(string[] args) 
  156.         { 
  157.             TBToList<Person> tol = new TBToList<Person>(); 
  158.             Console.WriteLine(); 
  159.             DataTable dt =  GetTable(); 
  160.             tol.ToList(dt); 
  161.             Console.Read(); 
  162.         } 
  163.         public static DataTable GetTable() 
  164.         { 
  165.             DataTable dt = new DataTable(); 
  166.             dt.Columns.Add("ID"); 
  167.             dt.Columns.Add("Age"); 
  168.             dt.Columns.Add("Lover"); 
  169.             dt.Columns.Add("Name"); 
  170.             DataRow dr = dt.NewRow(); 
  171.             dr["ID"] = 1; 
  172.             dr["Age"] = "Age1"
  173.             dr["Lover"] = "Lover1"
  174.             dr["Name"] = "Name1"
  175.             dt.Rows.Add(dr); 
  176.             DataRow dr1 = dt.NewRow(); 
  177.             dr1["ID"] = 2; 
  178.             dr1["Age"] = "Age2"
  179.             dr1["Lover"] = "Lover2"
  180.             dr1["Name"] = "Name2"
  181.             dt.Rows.Add(dr1); 
  182.             return dt; 
  183.         } 
  184.     } 

DataTable轉換成IList初版出來以後,昨晚老是以爲有不少地方能夠改進,因此今天一大早來就把它給修訂了,固然還有一些地方能夠改進,等我之後編碼能力提升以後再出第三版吧,第二版應該夠用orm

 對象

   
   
   
   
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Reflection; 
  4. using System.Collections; 
  5. using System.Data; 
  6. namespace JSONTest 
  7.     public class TableToList<T> where T : new() 
  8.     { 
  9.         /// <summary> 
  10.         /// DataTable轉換成IList 
  11.         /// </summary> 
  12.         /// <param name="dt"></param> 
  13.         /// <returns></returns> 
  14.         public IList<T> ToList(DataTable dt) 
  15.         { 
  16.             if (dt == null || dt.Rows.Count == 0) 
  17.             { 
  18.                 return null
  19.             } 
  20.             PropertyInfo[] properties = typeof(T).GetProperties();//獲取實體類型的屬性集合 
  21.             IList<string> colNames = GetColumnNames(dt.Columns);//按照屬性順序的列名集合 
  22.             List<T> list = new List<T>(); 
  23.             T model = default(T); 
  24.             foreach (DataRow dr in dt.Rows) 
  25.             { 
  26.                 model = new T();//建立實體 
  27.                 int i = 0; 
  28.                 foreach (PropertyInfo p in properties) 
  29.                 { 
  30.                     if (p.PropertyType == typeof(string)) 
  31.                     { 
  32.                         p.SetValue(model, dr[colNames[i++]], null); 
  33.                     } 
  34.                     else if (p.PropertyType == typeof(int)) 
  35.                     { 
  36.                         p.SetValue(model, int.Parse(dr[colNames[i++]].ToString()), null); 
  37.                     } 
  38.                     else if (p.PropertyType == typeof(bool)) 
  39.                     { 
  40.                         p.SetValue(model, bool.Parse(dr[colNames[i++]].ToString()), null); 
  41.                     } 
  42.                     else if (p.PropertyType == typeof(DateTime)) 
  43.                     { 
  44.                         p.SetValue(model, DateTime.Parse(dr[colNames[i++]].ToString()), null); 
  45.                     } 
  46.                     else if (p.PropertyType == typeof(float)) 
  47.                     { 
  48.                         p.SetValue(model, float.Parse(dr[colNames[i++]].ToString()), null); 
  49.                     } 
  50.                     else if (p.PropertyType == typeof(double)) 
  51.                     { 
  52.                         p.SetValue(model, double.Parse(dr[colNames[i++]].ToString()), null); 
  53.                     } 
  54.                 } 
  55.                  
  56.                 list.Add(model); 
  57.             } 
  58.             return list; 
  59.         } 
  60.  
  61.         /// <summary> 
  62.         /// 按照屬性順序的列名集合 
  63.         /// </summary> 
  64.         private IList<string> GetColumnNames(DataColumnCollection dcc) 
  65.         { 
  66.             PropertyInfo[] properties = typeof(T).GetProperties();//獲取實體類型的屬性集合 
  67.             //因爲集合中的元素是肯定的,因此能夠指定元素的個數,系統就不會分配多餘的空間,效率會高點 
  68.             IList<string> ilist = new List<string>(dcc.Count); 
  69.              
  70.             foreach (PropertyInfo p in properties) 
  71.             { 
  72.                 foreach (DataColumn dc in dcc) 
  73.                 { 
  74.                     if (dc.ColumnName.ToLower().Contains(p.Name.ToLower())) 
  75.                     { 
  76.                         ilist.Add(dc.ColumnName); 
  77.                     } 
  78.                 } 
  79.             } 
  80.             return ilist; 
  81.         } 
  82.   
  83.     } 
相關文章
相關標籤/搜索