(转载)DataTable与List<T>相互转换

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Linq;
 6 using System.Reflection;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9  
10 namespace Common
11 {
12     public class ModelHelper
13     {
14         public static List<T> TableToEntity<T>(DataTable dt) where T : new()
15         {
16             List<T> lists = new List<T>();
17             if (dt.Rows.Count > 0)
18             {
19                 foreach (DataRow row in dt.Rows)
20                 {
21                     lists.Add(SetVal(new T(), row));
22                 }
23             }
24             return lists;
25         }
26  
27         public static T SetVal<T>(T entity, DataRow row) where T : new()
28         {
29             Type type = typeof(T);
30             PropertyInfo[] pi = type.GetProperties();
31             foreach (PropertyInfo item in pi)
32             {
33                 if (row[item.Name] != null && row[item.Name] != DBNull.Value)
34                 {
35                     if (item.PropertyType.IsGenericType && item.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
36                     {
37                         Type conversionType = item.PropertyType;
38                         NullableConverter nullableConverter = new NullableConverter(conversionType);
39                         conversionType = nullableConverter.UnderlyingType;
40                         item.SetValue(entity, Convert.ChangeType(row[item.Name], conversionType), null);
41                     }
42                     else
43                     {
44                         item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
45                     }
46                 }
47             }
48             return entity;
49         }
50  
51         public static DataTable EntityToDataTable<T>(List<T> list) where T : new()
52         {
53             if (list == null || list.Count == 0)
54             {
55                 return null;
56             }
57  
58             DataTable dataTable = new DataTable(typeof(T).Name);
59             foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
60             {
61                 if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
62                 {
63                     Type conversionType = propertyInfo.PropertyType;
64                     NullableConverter nullableConverter = new NullableConverter(conversionType);
65                     conversionType = nullableConverter.UnderlyingType;
66                     dataTable.Columns.Add(new DataColumn(propertyInfo.Name, conversionType));
67                 }
68                 else
69                 {
70                     dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
71                 }
72             }
73  
74             foreach (T model in list)
75             {
76                 DataRow dataRow = dataTable.NewRow();
77                 foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
78                 {
79                     object value = propertyInfo.GetValue(model, null);
80                     if (value != null)
81                     {
82                         dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
83                     }
84                     else
85                     {
86                         dataRow[propertyInfo.Name] = DBNull.Value;
87                     }
88                 }
89                 dataTable.Rows.Add(dataRow);
90             }
91             return dataTable;
92         }
93     }
94 }
View Code

猜你喜欢

转载自www.cnblogs.com/hilolin/p/9098343.html