DataTable转换为List<Model>的通用类

 1 using System;  
 2 using System.Collections.Generic;  
 3 using System.Text;  
 4 using System.Data;  
 5 using System.Reflection;  
 6   
 7 namespace NCL.Data  
 8 {  
 9     /// <summary>  
10     /// 实体转换辅助类  
11     /// </summary>  
12     public class ModelConvertHelper<T> where  T : new()  
13     {  
14         public static IList<T> ConvertToModel(DataTable dt)  
15         {  
16             // 定义集合  
17             IList<T> ts = new List<T>();  
18   
19             // 获得此模型的类型  
20             Type type = typeof(T);  
21   
22             string tempName = "";  
23   
24             foreach (DataRow dr in dt.Rows)  
25             {  
26                 T t = new T();  
27   
28                 // 获得此模型的公共属性  
29                 PropertyInfo[] propertys = t.GetType().GetProperties();  
30   
31                 foreach (PropertyInfo pi in propertys)  
32                 {  
33                     tempName = pi.Name;  
34   
35                     // 检查DataTable是否包含此列  
36                     if (dt.Columns.Contains(tempName))  
37                     {  
38                         // 判断此属性是否有Setter  
39                         if (!pi.CanWrite) continue;  
40   
41                         object value = dr[tempName];  
42                         if (value != DBNull.Value)  
43                             pi.SetValue(t, value, null);  
44                     }  
45                 }  
46   
47                 ts.Add(t);  
48             }  
49   
50             return ts;  
51   
52         }  
53     }  
54 }  

 1   public static List<T> ConvertToModel(DataTable dt)
 2             {
 3                 if (dt == null || dt.Rows.Count == 0)
 4                 {
 5                     return null;
 6                 }
 7                 List<T> ts = new List<T>();
 8 
 9                 Type type = typeof(T);
10                 string tempName = "";
11 
12                 foreach (DataRow dr in dt.Rows)
13                 {
14                     T t = new T();
15 
16                     PropertyInfo[] propertys = t.GetType().GetProperties();
17                     foreach (PropertyInfo pi in propertys)
18                     {
19                         tempName = pi.Name;
20 
21                         if (dt.Columns.Contains(tempName))
22                         {
23 
24                             if (!pi.CanWrite)
25                             {
26                                 continue;
27                             }
28 
29                             object value = dr[tempName];
30                             setValue(ref t, pi, value);
31                         }
32                     }
33                     ts.Add(t);
34                 }
35                 return ts;
36             }

 

使用方式:

// 获得查询结果
DataTable dt = DbHelper.ExecuteDataTable(...);
// 把DataTable转换为IList<UserInfo>
IList<UserInfo> users = ModelConvertHelper<UserInfo>.ConvertToModel(dt);

posted @ 2017-07-04 17:27  suan1717  阅读(135)  评论(0编辑  收藏  举报