zoukankan      html  css  js  c++  java
  • C#将list转换为datatable

     1 DataTable dt = new DataTable();
     2 if (_list != null)
     3 {
     4       //通过反射获取list中的字段 
     5    System.Reflection.PropertyInfo[] p = _list[0].GetType().GetProperties();
     6    foreach (System.Reflection.PropertyInfo pi in p)
     7     {
     8       dt.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
     9     }
    10    for (int i = 0; i < _list.Count; i++)
    11    {
    12        IList TempList = new ArrayList();
    13         //将IList中的一条记录写入ArrayList
    14        foreach (System.Reflection.PropertyInfo pi in p)
    15        {
    16          object oo = pi.GetValue(_list[i], null);
    17          TempList.Add(oo);
    18        }
    19       object[] itm = new object[p.Length];
    20      for (int j = 0; j < TempList.Count; j++)
    21      {
    22        itm.SetValue(TempList[j], j);
    23      }
    24      dt.LoadDataRow(itm, true);
    25        }
    26 }
  • 相关阅读:
    python调用ggsci.exe程序
    confluence安装
    nginx优化
    ELKstack搭建
    zabbix 安装
    python requests
    小程序消息推送
    shell
    rar 解压
    ubuntu 安装部分设置U盘启动系统安装盘操作
  • 原文地址:https://www.cnblogs.com/lewisli/p/3449673.html
Copyright © 2011-2022 走看看