zoukankan
html css js c++ java
IList及泛型集合类转换DataTable
/**/
///
<summary>
///
将集合类转换成DataTable
///
</summary>
///
<param name="list">
集合
</param>
///
<returns></returns>
public
static
DataTable ToDataTable(IList list)
{
DataTable result
=
new
DataTable();
if
(list.Count
>
0
)
{
PropertyInfo[] propertys
=
list[
0
].GetType().GetProperties();
foreach
(PropertyInfo pi
in
propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
for
(
int
i
=
0
; i
<
list.Count; i
++
)
{
ArrayList tempList
=
new
ArrayList();
foreach
(PropertyInfo pi
in
propertys)
{
object
obj
=
pi.GetValue(list[i],
null
);
tempList.Add(obj);
}
object
[] array
=
tempList.ToArray();
result.LoadDataRow(array,
true
);
}
}
return
result;
}
/**/
///
<summary>
///
将泛型集合类转换成DataTable
///
</summary>
///
<typeparam name="T">
集合项类型
</typeparam>
///
<param name="list">
集合
</param>
///
<returns>
数据集(表)
</returns>
public
static
DataTable ToDataTable
<
T
>
(IList
<
T
>
list)
{
return
ConvertX.ToDataTable
<
T
>
(list,
null
);
}
/**/
///
<summary>
///
将泛型集合类转换成DataTable
///
</summary>
///
<typeparam name="T">
集合项类型
</typeparam>
///
<param name="list">
集合
</param>
///
<param name="propertyName">
需要返回的列的列名
</param>
///
<returns>
数据集(表)
</returns>
public
static
DataTable ToDataTable
<
T
>
(IList
<
T
>
list,
params
string
[] propertyName)
{
List
<
string
>
propertyNameList
=
new
List
<
string
>
();
if
(propertyName
!=
null
)
propertyNameList.AddRange(propertyName);
DataTable result
=
new
DataTable();
if
(list.Count
>
0
)
{
PropertyInfo[] propertys
=
list[
0
].GetType().GetProperties();
foreach
(PropertyInfo pi
in
propertys)
{
if
(propertyNameList.Count
==
0
)
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
else
{
if
(propertyNameList.Contains(pi.Name))
result.Columns.Add(pi.Name, pi.PropertyType);
}
}
for
(
int
i
=
0
; i
<
list.Count; i
++
)
{
ArrayList tempList
=
new
ArrayList();
foreach
(PropertyInfo pi
in
propertys)
{
if
(propertyNameList.Count
==
0
)
{
object
obj
=
pi.GetValue(list[i],
null
);
tempList.Add(obj);
}
else
{
if
(propertyNameList.Contains(pi.Name))
{
object
obj
=
pi.GetValue(list[i],
null
);
tempList.Add(obj);
}
}
}
object
[] array
=
tempList.ToArray();
result.LoadDataRow(array,
true
);
}
}
return
result;
}
查看全文
相关阅读:
Spring中bean的生命周期
HashMap与HashTable原理及数据结构
HashMap 与HashTable的区别
2 rocketmq mqadmin 的用法详解
搭建RocketMQ踩的坑-内存不足
解决Hash碰撞冲突方法总结
大端BigEndian、小端LittleEndian与字符集编码
Linux中最常用的JAVA_HOME配置
WIP_DISCRETE_JOBS.STATUS_TYPE
Inventory > INV.MTL_MATERIAL_TRANSACTIONS Show Error Msg: ORA-20100: File lxxx.tmp creation for FND_FILE failed.
原文地址:https://www.cnblogs.com/zhahost/p/1212470.html
最新文章
2016年中资管机构规模排名哪家强(名单)
delete
重磅榜单!互联网金融Top100总估值超1.1万亿,27家独角兽上榜!
delete
2016年300多家银行资产规模排名
20170429,上市公司2016年报全出炉(附最新排行榜)
Chrome快捷键, Mac 下 Chrome 浏览器 快捷键
什么样的女人适合做老婆
各国英文名称
jQuery find
热门文章
jQuery file upload里面的_create的调用和_initEventHandlers的调用
How jQuery UI Works
How to Create a Basic Plugin 如何写一个基础的jQuery插件
npm-package-lock.json
jQuery file upload cropper的 click .preview事件没有绑定成功
jQuery file upload cropper的流程
jQuery file upload上传图片的流程
chrome查看JavaScript的堆栈调用
Spring Bean详细讲解
Spring中Bean及@Bean的理解
Copyright © 2011-2022 走看看