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;
            }

  • 相关阅读:
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    网页排版中的浮动和定位(学习笔记)
    在html中,<input tyle = "text">除了text外还有几种种新增的表单元素
    初学者入门web前端:C#基础知识:函数
    初学者入门web前端 C#基础知识:数组与集合
    while/for循环
    jmeter http请求与参数化
    rpm -e --nodeps
  • 原文地址:https://www.cnblogs.com/xiguanjiandan/p/3158409.html
Copyright © 2011-2022 走看看