码迷,mamicode.com
首页 > 其他好文 > 详细

ListToDataTable

时间:2016-12-24 22:30:42      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:datarow   turn   collect   object   cti   info   return   i++   type   

public static DataTable ToDataTable<T>(IEnumerable<T> collection)
       {
           var props = typeof(T).GetProperties();
           var dt = new DataTable();
           dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
           if (collection.Count() > 0)
           {
               for (int i = 0; i < collection.Count(); i++)
               {
                   ArrayList tempList = new ArrayList();
                   foreach (PropertyInfo pi in props)
                   {
                       object obj = pi.GetValue(collection.ElementAt(i), null);
                       tempList.Add(obj);
                   }
                   object[] array = tempList.ToArray();
                   dt.LoadDataRow(array, true);
               }
           }
           return dt;
       }

ListToDataTable

标签:datarow   turn   collect   object   cti   info   return   i++   type   

原文地址:http://www.cnblogs.com/jiaohuifeng/p/6218546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!