`

List转DataTable 反射方式

    博客分类:
  • C#
阅读更多

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

分享到:
评论

相关推荐

    .net 将datatable转换为实体类LIST

    将datatable转换为实体类LIST,运用了反射和泛型的技术

    DataTable转List

    利用反射和泛型 把Datatable转化为List 1

    将DataTable转换成Listlt;Tgt;实现思路及示例代码

    前几天在工作中,遇到一个问题:需要将查询出来的DataTable数据源,转换成List的泛型集合(已知T类型)。第一反应,我想肯定要用到“泛型”(这不是废话吗?都说了要转换成List泛型集合了),而且还要用到“反射”...

    c#中利用委托反射将DataTable转换为实体集的代码

    类泛型的约束: 代码如下: public static class ToModel&lt;T&gt; where T : class, new() 定义委托: 代码如下:public delegate void SetString(string value); 创建委托方法: 代码如下: private static SetString ...

    C#将DataTable转化为ListT

    在使用三层架构开发一个网站时,希望把DataTable对象转换为List对象,于是在网上找资料,总结一个比较方便的方法来实现——使用反射。 思路: 初始化一个List对象 获取到T所有的属性,初始化一个T对象 遍历所有属性...

    NPOI 导入导出Excel (List泛型版)

    NPOL网上有返回DataTable,我自己编写了个泛型版的。

    C#中反射和扩展方法如何运用

    前段时间做了一个练手的小项目,名叫Book_Bar,用来卖书的,采用的是三层架构,也就是Models,IDAL,DAL,BLL 和 Web , 在DAL层中各个类中有一个方法比较常用,那就是RowToClass ,顾名思义,也就是将DataTable 中...

    NPOI.ExcelReaderHelper.rar

    此帮助类为NPOI读取读取EXCEL的帮助类,其中.Net Framework版本为4.6,NPOI版本为2.4.1.0,通过NPOI读取EXCEL之后,使用反射的方式将对应的Datatable转换为相应的List,具体可参考附件中代码

    ASP.Net MVC+Data Table实现分页+排序功能的方法

    使用datatable内置的分页,排序 使用attribute+反射来控制需要排序和显示的字段以及顺序 分离排序和显示逻辑 若要添加搜索逻辑只需要传递搜索的字段到后端即可(js初始化时把”searching”: false拿掉)。 View : @...

    .net导出数据到excel

    一个.net将数据导出到excel的源码,用反射机制导出List到excel,也可将DataTable导出到excel,样式可以灵活修改

    aspnet公共类cs文件.rar

    反射操作辅助类,如获取或设置字段、属性的值等反射信息。(ReflectionUtil.cs) 注册表操作辅助类(RegistryHelper.cs) 用于验证码图片识别的类(UnCodebase.cs) 将原始字串转换为unicode,格式为\u.\u.( ...

    WHC第三方控件

    12. 反射操作辅助类,如获取或设置字段、属性的值等反射信息。(ReflectionUtil.cs) 13. 注册表操作辅助类(RegistryHelper.cs) 14. 用于验证码图片识别的类(UnCodebase.cs) 15. 将原始字串转换为unicode,格式为\u...

    DotNet公用类(超多附文档)

    12.反射操作辅助类,如获取或设置字段、属性的值等反射信息。(ReflectionUtil.cs) 13.注册表操作辅助类(RegistryHelper.cs) 14.用于验证码图片识别的类(UnCodebase.cs) 15.将原始字串转换为unicode,格式为\u.\u....

    asp.net知识库

    如何判断ArrayList,Hashtable,SortedList 这类对象是否相等 帮助解决网页和JS文件中的中文编码问题的小工具 慎用const关键字 装箱,拆箱以及反射 动态调用对象的属性和方法——性能和灵活性兼备的方法 消除由try/...

    ASP.NET 2.0+SQL Server 2005全程指南-源代码

    3.1.4 CheckBOX和CheckBoxList控件应用 3.1.5 RadioButton和RadioButtonList控件应用 3.2 ASP.NET 2.0新增控件 3.2.1 BulletedList控件的应用 3.2.2 Substitution控件的应用 3.2.3 Wizard控件的应用 3.2.4 ...

Global site tag (gtag.js) - Google Analytics