zoukankan      html  css  js  c++  java
  • List转DataTable

            public static DataTable ListToDT<T>(List<T> ls)
            {
                DataTable dt = new DataTable();
                Type tp = typeof(T);
                PropertyInfo[] pinfo = tp.GetProperties();

                foreach (PropertyInfo pi in pinfo)
                {
                    dt.Columns.Add(pi.Name);
                   
                }

                if (ls != null)
                {
                    for (int i = 0; i < ls.Count; i++)
                    {
                        IList TempList = new ArrayList();
                        foreach (System.Reflection.PropertyInfo pi in pinfo)
                        {
                            object oo = pi.GetValue(ls[i], null);
                            TempList.Add(oo);
                        }

                        object[] itm = new object[pinfo.Length];
                        //遍历ArrayList向object[]里放数据
                        for (int j = 0; j < TempList.Count; j++)
                        {
                            itm.SetValue(TempList[j], j);
                        }
                        //将object[]的内容放入DataTable
                        dt.LoadDataRow(itm, true);
                    }

                }

                return dt;
            }

  • 相关阅读:
    pycharm 安装第三方库报错:AttributeError: 'module' object has no attribute 'main'
    工作冥想
    对于测试工作与测试人员未来出路的思考
    测试计划再谈
    python 反转列表的3种方式
    关于最近练习PYTHON代码的一点心得
    python sum()函数的用法
    python count()函数
    SpringCloud和SpringBoot的详细版本说明
    使用 lntelliJ IDEA 创建 Maven 工程的springboot项目
  • 原文地址:https://www.cnblogs.com/xiguanjiandan/p/3158409.html
Copyright © 2011-2022 走看看