zoukankan
html css js c++ java
IList<T> 转换成 DataSet
通过 NHibernate 查询返回的表数据通常都是
IList<T>
,但在实际使用中有些数据控件在数据绑定上对IList的支持并不是很好,所以有时候将IList转换成DataSet还是有必要的。在
Vinson的Blogs
中提到了怎么
将IList转换成DataSet
,可惜源码是VB.NET的。
下面是我根据
IList转换成DataSet
中的VB.NET源码转换成C#,由于现在做项目是用 VS 2005 ,所以也对其改进,让它支持C#2.0 中的泛型。
代码如下:
using
System;
using
System.Data;
public
class
NHibernateHelper
{
/**/
///
<summary>
///
Ilist
<T>
转换成 DataSet
///
</summary>
///
<param name="list"></param>
///
<returns></returns>
public
static
DataSet ConvertToDataSet
<
T
>
(IList
<
T
>
list)
{
if
(list
==
null
||
list.Count
<=
0
)
{
return
null
;
}
DataSet ds
=
new
DataSet();
DataTable dt
=
new
DataTable(
typeof
(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo
=
typeof
(T).GetProperties(System.Reflection.BindingFlags.Public
|
System.Reflection.BindingFlags.Instance);
foreach
(T t
in
list)
{
if
(t
==
null
)
{
continue
;
}
row
=
dt.NewRow();
for
(
int
i
=
0
, j
=
myPropertyInfo.Length; i
<
j; i
++
)
{
System.Reflection.PropertyInfo pi
=
myPropertyInfo[i];
string
name
=
pi.Name;
if
(dt.Columns[name]
==
null
)
{
column
=
new
DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}
row[name]
=
pi.GetValue(t,
null
);
}
dt.Rows.Add(row);
}
ds.Tables.Add(dt);
return
ds;
}
}
查看全文
相关阅读:
正态分布与中心极限定理
超几何分布与二项分布及其期望
cf492E. Vanya and Field(扩展欧几里得)
ZR#317.【18 提高 2】A(计算几何 二分)
小米OJ刷题日志
cf519D. A and B and Interesting Substrings(前缀和)
cf519C. A and B and Team Training(找规律)
BZOJ2118: 墨墨的等式(最短路 数论)
Service生命周期图
python2.7中使用mysql (windows XP)
原文地址:https://www.cnblogs.com/yyw84/p/463480.html
最新文章
ALM11测试计划页面图解1
ALM11用例测试类型
ALM11 OTA API接口的问题
Quality Center 使用IE8异常浏览器打开
Quality Center配置邮箱服务
SMTP邮件服务器配置
Quality Center11初始化失败
jQuery基础学习8——层次选择器next()和prev()方法
jQuery基础学习8——层次选择器children()方法
Win7电脑无法启用无线连接或无线连不上网
热门文章
VisualStudio基本使用(2)-使用VS2013来编译C语言程序
VisualStudio基本使用(1)-显示行号
QQ空间HD(4)-设置左侧菜单栏属性
QQ空间HD(3)-Modal的切换效果总结
QQ空间HD(2)-UIPopoverController其它使用
iOS- Could not find a storyboard named 'Main' in bundle NSBundle
QQ空间HD(1)-UIPopoverController基本使用
Xcode常用技巧(2)-使Xcode在创建类时自动添加前缀
[牛客OI测试赛2]F假的数学游戏(斯特灵公式)
二项式定理
Copyright © 2011-2022 走看看