zoukankan      html  css  js  c++  java
  • List<T>转DataTable


     

     1   public static class DataConvertor
     2     {
     3         public static DataTable ToDataTable<T>(IEnumerable<T> data)
     4         {
     5             PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
     6             var table = new DataTable();
     7             foreach (PropertyDescriptor prop in properties)
     8                 table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
     9             foreach (T item in data)
    10             {
    11                 DataRow row = table.NewRow();
    12                 foreach (PropertyDescriptor prop in properties)
    13                     row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
    14                 table.Rows.Add(row);
    15             }
    16             return table;
    17         }
    18         
    19         public static DataTable ToDataTable(IRfcTable rfcTable)
    20         {
    21             DataTable table = new DataTable();
    22             int liElement = 0;
    23             for (liElement = 0; liElement <= rfcTable.ElementCount - 1; liElement++)
    24             {
    25                 RfcElementMetadata metadata = rfcTable.GetElementMetadata(liElement);
    26                 table.Columns.Add(metadata.Name); //循环创建列
    27             }
    28             foreach (IRfcStructure dr in rfcTable) //循环table结构表
    29             {
    30                 DataRow row = table.NewRow(); //创建新行
    31                 for (liElement = 0; liElement <= rfcTable.ElementCount - 1; liElement++)
    32                 {
    33                     RfcElementMetadata metadata = rfcTable.GetElementMetadata(liElement);
    34                     row[metadata.Name] = dr.GetString(metadata.Name).Trim();
    35                 }
    36                 table.Rows.Add(row);
    37             }
    38 
    39             return table;
    40         }
    41     }
  • 相关阅读:
    Spring 学习笔记
    Java Web整合开发(33) -- SSH和SSJ
    2、常用操作
    jsonp使用
    PHP curl 封装 GET及POST方法很不错的
    浅谈CSRF攻击方式 转
    谷歌插件请求ci 解决CI框架的Disallowed Key Characters错误提示
    phpstorm10.0.3 下载与激活
    Mysql全文搜索match against的用法
    CentOS 6.4下编译安装MySQL 5.6.14 (转)
  • 原文地址:https://www.cnblogs.com/yyx999/p/13409865.html
Copyright © 2011-2022 走看看