List转换为DataTable List<Entity>

       /// <summary>
       /// 将List转换成DataTable
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="data"></param>
       /// <returns></returns>
       public static DataTable ToDataTable<T>(thisIList<T> data)
           {
           PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
           DataTable dt = newDataTable();
           for(inti = 0; i < properties.Count; i++)
               {
               PropertyDescriptor property = properties[i];
               dt.Columns.Add(property.Name, property.PropertyType);
               }
           object[] values = newobject[properties.Count];
           foreach(T item indata)
               {
               for(inti = 0; i < values.Length; i++)
                   {
                   values[i] = properties[i].GetValue(item);
                   }
               dt.Rows.Add(values);
               }
           return dt;
           }

 

posted on 2017-06-08 11:15  选择大于努力  阅读(499)  评论(0编辑  收藏  举报

导航