惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
腾讯CDC
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
Vercel News
Vercel News
P
Proofpoint News Feed
雷峰网
雷峰网
博客园_首页
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
F
Fortinet All Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题

博客园 - 农村的芬芳

PHP使用第三篇:生成数据库 使用THINKPHP产生的:No database selected [问题 从今天开始记录PHP使用的点点滴滴 留给自个看的工具提示 转xml Oracle 跨库 查询 复制表数据 关于海量数据处理 近日用到啦progressbar控件,将其用法留一下 select into 和 insert into select 两种表复制语句 采用regsvr32注册组件后提示:没有注册 将对象数组转换成dataset windows 服务操作 转载:XML与DataSet的相互转换类 oracle 日期函数小计 如何创建自定义帐户来运行 ASP.NET 通过ASP.net程序创建域帐户故障 EnterpriseLibrary服务问题 下载文件关闭窗体之解决方法 超级郁闷之问题,请DUDU及各位大位指正错误
net2005中将list<>数组转换为Table
农村的芬芳 · 2007-11-16 · via 博客园 - 农村的芬芳

 public static DataTable GetDataTableFromIList<T>(List<T> aIList)
        {

            DataTable _returnTable = new DataTable();

            if (aIList.Count > 0)
            {

                //Creates the table structure looping in the in the first element of the list

                object _baseObj = aIList[0];

                Type objectType = _baseObj.GetType();

                PropertyInfo[] properties = objectType.GetProperties();

                DataColumn _col;

                foreach (PropertyInfo property in properties)
                {

                    _col = new DataColumn();

                    _col.ColumnName = (string)property.Name;

                    _col.DataType = property.PropertyType;

                    _returnTable.Columns.Add(_col);

                }

                //Adds the rows to the table

                DataRow _row;

                foreach (object objItem in aIList)
                {

                    _row = _returnTable.NewRow();

                    foreach (PropertyInfo property in properties)
                    {
                        _row[property.Name] = property.GetValue(objItem, null);
                    }
                    _returnTable.Rows.Add(_row);

                }

            }

            return _returnTable;

        }