List转DataTable

  1 public static class DataTableExtensions
  2 {
  3     ///   
  4  
  5     /// 转化一个DataTable  
  6  
  7     ///   
  8  
  9     ///   
 10     ///   
 11     ///   
 12     public static DataTable ToDataTable(this IEnumerable list)
 13     {
 14  
 15         //创建属性的集合  
 16         List pList = new List();
 17         //获得反射的入口  
 18  
 19         Type type = typeof(T);
 20         DataTable dt = new DataTable();
 21         //把所有的public属性加入到集合 并添加DataTable的列  
 22         Array.ForEach(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
 23         foreach (var item in list)
 24         {
 25             //创建一个DataRow实例  
 26             DataRow row = dt.NewRow();
 27             //给row 赋值  
 28             pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
 29             //加入到DataTable  
 30             dt.Rows.Add(row);
 31         }
 32         return dt;
 33     }
 34  
 35  
 36     ///   
 37     /// DataTable 转换为List 集合  
 38     ///   
 39     /// 类型  
 40     /// DataTable  
 41     ///   
 42     public static List ToList(this DataTable dt) where T : class, new()
 43     {
 44         //创建一个属性的列表  
 45         List prlist = new List();
 46         //获取TResult的类型实例  反射的入口  
 47  
 48         Type t = typeof(T);
 49  
 50         //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表   
 51         Array.ForEach(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });
 52  
 53         //创建返回的集合  
 54  
 55         List oblist = new List();
 56  
 57         foreach (DataRow row in dt.Rows)
 58         {
 59             //创建TResult的实例  
 60             T ob = new T();
 61             //找到对应的数据  并赋值  
 62             prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });
 63             //放入到返回的集合中.  
 64             oblist.Add(ob);
 65         }
 66         return oblist;
 67     }
 68  
 69  
 70  
 71  
 72     ///   
 73     /// 将集合类转换成DataTable  
 74     ///   
 75     /// 集合  
 76     ///   
 77     public static DataTable ToDataTableTow(IList list)
 78     {
 79         DataTable result = new DataTable();
 80         if (list.Count > 0)
 81         {
 82             PropertyInfo[] propertys = list[0].GetType().GetProperties();
 83  
 84             foreach (PropertyInfo pi in propertys)
 85             {
 86                 result.Columns.Add(pi.Name, pi.PropertyType);
 87             }
 88             for (int i = 0; i < list.Count; i++)
 89             {
 90                 ArrayList tempList = new ArrayList();
 91                 foreach (PropertyInfo pi in propertys)
 92                 {
 93                     object obj = pi.GetValue(list[i], null);
 94                     tempList.Add(obj);
 95                 }
 96                 object[] array = tempList.ToArray();
 97                 result.LoadDataRow(array, true);
 98             }
 99         }
100         return result;
101     }
102  
103  
104     /**/
105  
106     ///   
107     /// 将泛型集合类转换成DataTable  
108  
109     ///   
110     /// 集合项类型  
111  
112     /// 集合  
113     /// 数据集(表)  
114     public static DataTable ToDataTable(IList list)
115     {
116         return ToDataTable(list, null);
117  
118     }
119  
120  
121     /**/
122  
123     ///   
124     /// 将泛型集合类转换成DataTable  
125     ///   
126     /// 集合项类型  
127     /// 集合  
128     /// 需要返回的列的列名  
129     /// 数据集(表)  
130     public static DataTable ToDataTable(IList list, params string[] propertyName)
131     {
132         List<string> propertyNameList = new List<string>();
133         if (propertyName != null)
134             propertyNameList.AddRange(propertyName);
135         DataTable result = new DataTable();
136         if (list.Count > 0)
137         {
138             PropertyInfo[] propertys = list[0].GetType().GetProperties();
139             foreach (PropertyInfo pi in propertys)
140             {
141                 if (propertyNameList.Count == 0)
142                 {
143                     result.Columns.Add(pi.Name, pi.PropertyType);
144                 }
145                 else
146                 {
147                     if (propertyNameList.Contains(pi.Name))
148                         result.Columns.Add(pi.Name, pi.PropertyType);
149                 }
150             }
151  
152             for (int i = 0; i < list.Count; i++)
153             {
154                 ArrayList tempList = new ArrayList();
155                 foreach (PropertyInfo pi in propertys)
156                 {
157                     if (propertyNameList.Count == 0)
158                     {
159                         object obj = pi.GetValue(list[i], null);
160                         tempList.Add(obj);
161                     }
162                     else
163                     {
164                         if (propertyNameList.Contains(pi.Name))
165                         {
166                             object obj = pi.GetValue(list[i], null);
167                             tempList.Add(obj);
168                         }
169                     }
170                 }
171                 object[] array = tempList.ToArray();
172                 result.LoadDataRow(array, true);
173             }
174         }
175         return result;
176     }
177 }
View Code

你可能感兴趣的:(List转DataTable)