List转换DataTable
/// <summary>
/// 将泛类型集合List类转换成DataTable
/// </summary>
/// <param name="list">泛类型集合</param>
/// <returns></returns>
public static DataTable ListToDataTable<T>(List<T> entitys)
{
//检查实体集合不能为空
if (entitys == null || entitys.Count < 1)
{
throw new Exception("需转换的集合为空");
}
//取出第一个实体的所有Propertie
Type entityType = entitys[0].GetType();
PropertyInfo[] entityProperties = entityType.GetProperties(); //生成DataTable的structure
//生产代码中,应将生成的DataTable结构Cache起来,此处略
DataTable dt = new DataTable();
for (int i = 0; i < entityProperties.Length; i++)
{
//dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
dt.Columns.Add(entityProperties[i].Name);
}
//将所有entity添加到DataTable中
foreach (object entity in entitys)
{
//检查所有的的实体都为同一类型
if (entity.GetType() != entityType)
{
throw new Exception("要转换的集合元素类型不一致");
}
object[] entityValues = new object[entityProperties.Length];
for (int i = 0; i < entityProperties.Length; i++)
{
entityValues[i] = entityProperties[i].GetValue(entity, null);
}
dt.Rows.Add(entityValues);
}
return dt;
}
List转换DataTable的更多相关文章
- dataGrid转换dataTable
#region dataGrid转换dataTable /// <summary> /// dataGrid转换dataTable /// </summary> ...
- C# 将Excel以文件流转换DataTable
/* *引用 NuGet包 Spire.XLS */ /// <summary> /// Excel帮助类 /// </summary> public class ExcelH ...
- List<T> 转换 DataTable
public class List2DataTable { public static string GetClassName(Type type) { if (typ ...
- DataRow数组转换DataTable
public DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; Da ...
- C# DataRow[]转换DataTable
DataTable dt = ... DataRow[] dr = dt.Select("ID=14"); dt = dr.CopyToDataTable();
- c# 将csv文件转换datatable的两种方式。
第一种: public static DataTable csvdatatable(string path) { DataTable dt = new DataTable(); string conn ...
- 实体lis<T>t转换datatable
public static DataTable ListToTable<T>(List<T> list) { Type type = typeof(T) ...
- 读CSV转换datatable
using System.Data; using System.IO; /// <summary> /// Stream读取.csv文件 /// </summary> // ...
- SqL读取XML、解析XML、SqL将XML转换DataTable、SqL将XML转换表
DECLARE @ItemMessage XML )) SET @ItemMessage=N' <ReceivablesInfos> <ReceivablesList> < ...
随机推荐
- socket+mysql
测试windows 下 Socket+Mysql调用. 环境: OS:Win7; DBMS:Mysql; IDE:codeblockS. server: #include <WINSOCK2. ...
- vim:用vim修改文件编码为utf-8
命令是 :set fileencoding=utf-8如果用vim打开文件时里面有乱码,可能用上面的命令修改文件后无法保存.可以用其他软件打开文件,然后把内容拷贝到vim里再保存就行了.
- xss利用和检测平台
xssing 是安全研究者Yaseng发起的一个基于 php+mysql的 网站 xss 利用与检测开源项目,可以对你的产品进行黑盒xss安全测试,可以兼容获取各种浏览器客户端的网站url,cooki ...
- python 占位符 %s Format
1.百分号方式 %[(name)][flags][width].[precision]typecode (name) 可选,用于选择指定的key flags 可选,可供选择 ...
- Java并发编程核心方法与框架-ScheduledExecutorService的使用
类SchedukedExecutorService的主要作用是可以将定时任务与线程池功能结合. 使用Callable延迟运行(有返回值) public class MyCallableA implem ...
- 经纬度距离计算Java实现代码
public class test { private static double rad(double d) { return d * Math.PI / 180.0; } public stati ...
- memcached的最佳实践方案(转)
基本问题 1.memcached的基本设置 1)启动Memcache的服务器端 # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 ...
- 使用ab压测网页结果分析
使用ab压测网页结果分析 下载工具:ab 图片来自:http://my.oschina.net/u/1246814/blog/291696?fromerr=JfLeu1jk
- 使用微信JS-SDK调用微信浏览器的接口
先附上微信公众平台的相关链接: 微信公众平台:https://mp.weixin.qq.com/ 微信公众平台开发文档:https://mp.weixin.qq.com/wiki 微信公众平台JS-S ...
- SQLSERVER2008R2正确使用索引
T1表 10000000万条数据,(插入时间36分钟,count(*)查询19秒,空间占用670M左右) 1.真正充分的利用索引比如like '张%' 就是符合SARG(符合扫描参数)标准而like ...