首页 > 其他 > 详细

List转换DataTable

时间:2014-07-14 22:59:16      阅读:385      评论:0      收藏:0      [点我收藏+]
 1 public static class IListUtil
 2 {
 3     /// <summary>
 4     /// 将集合类转换成DataTable 
 5     /// </summary>
 6     /// <param name="list">集合</param>
 7     /// <returns></returns>
 8     public static DataTable AsDataTable<T>(this IList<T> list)
 9     {
10         DataTable result = new DataTable();
11         if (list.Count > 0)
12         {
13             PropertyInfo[] propertys = typeof(T).GetProperties();
14             foreach (PropertyInfo pi in propertys)
15             {
16                 result.Columns.Add(pi.Name, pi.PropertyType);
17             }
18 
19             for (int i = 0; i < list.Count; i++)
20             {
21                 ArrayList tempList = new ArrayList();
22                 foreach (var item in propertys)
23                 {
24                     object obj = item.GetValue(list[i], null);
25                     tempList.Add(obj);
26                 }
27 
28                 object[] array = tempList.ToArray();
29                 result.LoadDataRow(array, true);
30             }
31         }
32         return result;
33     }
34 
35 
36 }

 

List转换DataTable,布布扣,bubuko.com

List转换DataTable

原文:http://www.cnblogs.com/gaobing/p/3842340.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!