public static DataTable ToDataTableTow(IList list)
{
DataTable result = new DataTable();
if (list.Count > )
{
PropertyInfo[] propertys = list[].GetType().GetProperties(); foreach (PropertyInfo pi in propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
for (int i = ; 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>
/// 泛型转换list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(IList<T> list)
{
return 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 > )
{
PropertyInfo[] propertys = list[].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
else
{
if (propertyNameList.Contains(pi.Name))
result.Columns.Add(pi.Name, pi.PropertyType);
}
} for (int i = ; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
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;
}

List,泛型和Datatable 的相互转换的更多相关文章

  1. 机房重构——泛型和“DataTable”

    前言 我们都知道在机房重构的时候,大多数都在用七层进行重构,每一层都依赖实体.所以不管怎么调用,返回的应该是实体参数,这样才符合大多数的逻辑,这样我们试想一下,如果我们要求在U层返回多个实体值,怎么办 ...

  2. 泛型和DataTable的属性

    泛型转DataTable public DataTable ToDataTable<TResult>(this IEnumerable<TResult> value) wher ...

  3. Java泛型学习笔记--Java泛型和C#泛型比较学习(一)

    总结Java的泛型前,先简单的介绍下C#的泛型,通过对比,比较学习Java泛型的目的和设计意图.C#泛型是C#语言2.0和通用语言运行时(CLR)同时支持的一个特性(这一点是导致C#泛型和Java泛型 ...

  4. 泛型和 Any 类型

    泛型和 Any 类型 这两个类型看起来很相似,但是一定要小心两者的区别.他们区别在于 Any 类型会避开类型的检查,所以尽量少用最好不用.泛型一方面很灵活一方面也很安全,下面举个例子感受下两者的区别: ...

  5. java 泛型和object比较

    引言 我们使用object和泛型做形参,都是为了让这个方法能接收更多类型的对象,让程序变得更健壮,代码复用率更高.当我们回看自己写的代码时会发现,好像使用泛型的地方使用object也可以,使用obje ...

  6. C#的泛型和Java的伪泛型

    C#的泛型和java的伪泛型,talk is cheap,show me the code   C#泛型 下面结果,C#里面会输出false,如果这个还不能真正的说明C#的泛型是真的泛型,那就看下面这 ...

  7. Newtonsoft.Json 与 DataTable的相互转换

    1.这里下载:http://www.newtonsoft.com/products/json/ 安装:    解压下载文件,得到Newtonsoft.Json.dll    在项目中添加引用 2.引入 ...

  8. List<T> 和DataTable的相互转换

    我用的将集合类转换为DataTable 的方法 /// <summary> /// 将集合类转换成DataTable /// </summary> /// <param ...

  9. C# List和DataTable的相互转换

    1.List转DataTable /// <summary> /// list to datatable /// </summary> /// <typeparam na ...

随机推荐

  1. Windows Server 2012 配置远程桌面帐户允许多用户同时登录

    网上找了很多关于设置远程桌面最大连接数的文章,大都是说先要到控制面板的管理工具中设置远程桌面会话主机等,大体和我之前的文章<设置WINDOWS SERVER 2008修改远程桌面连接数>里 ...

  2. IIS Express(7.0) HTTP 错误 500.22 - Internal Server Error(vs2013)

    1.错误如下: HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置. 解决的方法: 首先,找到本地appcmd.x ...

  3. Java:ConcurrentHashMap支持完全并发的读

    ConcurrentHashMap完全允许多个读操作并发进行,读操作并不需要加锁.(事实上,ConcurrentHashMap支持完全并发的读以及一定程度并发的写.)如果使用传统的技术,如HashMa ...

  4. centos7制作本地yum源

    创建想要挂载的路径 mkdir /mnt/cdrom 挂载本地镜像到创建的目录 mount -t iso9660 /dev/cdrom /mnt/cdrom/ mount: /dev/sr0 is w ...

  5. ABP学习之旅

    1.我使用ABP的启动模板(http://www.aspnetboilerplate.com/Templates)来创建一个Web应用程序. 2.加载项目解决方案 在abp根据模板创建解决方案后,编译 ...

  6. centos7 防火墙相关命令

    启动:systemctl start firewalld禁用:systemctl stop firewalld重新载入规则:firewall-cmd --reload查看所有打开的端口:firewal ...

  7. 《Linux就该这么学》第十三天课程

    使用Apache服务部署静态网站 原创地址:https://www.linuxprobe.com/chapter-10.html 今天学了Apache,这只是RHCE课程的开始,估计后面越来越难 今天 ...

  8. vuex创建store并用computed获取数据

    vuex中的store是一个状态管理器,用于分发数据.相当于父组件数据传递给子组件. 1.安装vuex npm i vuex --save 2.在src目录中创建store文件夹,里面创建store. ...

  9. spring 5.1.2 mvc RequestMappingHandlerMapping 调用handler过程

    https://my.oschina.net/zhangxufeng/blog/2177464 https://www.jianshu.com/p/447826c28e37 Interceptors ...

  10. 【Golang】如何统一处理HTTP请求中的异常捕获

    最近写GOLANG项目,不使用框架,路由选择httprouter 现在想实现一个需求:在不修改httprouter源码的前提下,对所有注册的路由handle进行异常捕获. 大家都知道golang使用p ...