List<T> 转换 DataTable
public class List2DataTable {
public static string GetClassName(Type type)
{ if (type == null)
throw new ArgumentException("参数type不能为空");
if (type.HasAttribute<AliasAttribute>())
return type.GetAttribute<AliasAttribute>().Name.ToLower();
else
return type.Name.ToLower();
}
public static DataTable ListToDataTable<T>(List<T> oList) where T : class
{
try
{
string tableName = GetClassName(typeof(T));
DataTable dt = new DataTable();
string fieldName;
object fieldValue;
string[] fieldNames;
System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();
fieldNames = new string[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
fieldName = Props[i].Name;
fieldNames[i] = fieldName;
dt.Columns.Add(fieldName, Props[i].PropertyType);
}
for (int r = 0; r < oList.Count; r++)
{
DataRow dr = dt.NewRow();
T o = oList[r];
for (int i = 0; i < fieldNames.Length; i++)
{
fieldValue = Props[i].GetValue(o, null);
dr[fieldNames[i]] = fieldValue;
}
dt.Rows.Add(dr);
}
return dt;
}
catch(Exception ex) { }
return null;
}
}
List<T> 转换 DataTable的更多相关文章
- dataGrid转换dataTable
#region dataGrid转换dataTable /// <summary> /// dataGrid转换dataTable /// </summary> ...
- C# 将Excel以文件流转换DataTable
/* *引用 NuGet包 Spire.XLS */ /// <summary> /// Excel帮助类 /// </summary> public class ExcelH ...
- List转换DataTable
/// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...
- 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> < ...
随机推荐
- Rest API 开发 学习笔记(转)
Rest API 开发 学习笔记 概述 REST 从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表示方式.获得这些表徵致使这些应用程序转变了其状态.随着 ...
- java中的集合和数组
数组Array和集合的区别: (1)数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型) (2)JAVA集合可以存储和操作数目不固定的一组数据. (3)若程序时不知道究竟需要多 ...
- extjs 箱子布局
a.flex 配置项 flex 配置项不是设置在布局上,而是设置在子项的配置项.每个子项相对的 flex 值都会与全体子项 flex 累加的值相比较,根据此结果,处理每个子项的 flex 最后是多少. ...
- iOS开发小技巧--巧用ImageView中的mode(解决图片被拉伸的情况)
一.自己遇到的问题:在布局ImageView的时候,通过约束将ImageView布局好,但是里面的图片被拉伸的很难看.这时候就用到了Mode属性,如图: 代码实现方式: 二.让图片按照比例拉伸,并不是 ...
- 【CodeVS 3123】高精度练习之超大整数乘法 &【BZOJ 2197】FFT快速傅立叶
第一次写法法塔,,,感到威力无穷啊 看了一上午算导就当我看懂了?PS:要是机房里能有个清净的看书环境就好了 FFT主要是用了巧妙的复数单位根,复数单位根在复平面上的对称性使得快速傅立叶变换的时间复杂度 ...
- css 导航,菜单对应页面切换效果实现方法
实现原理: 每个菜单有多个li标签,每个li标签含一个id,li标签的id用来标记:点击效果 每个页面有一个id,这个id的作用是对应每个li标签的点击链接对应的页面,它的作用是用来标记:li标签的h ...
- java.lang.Exception: No runnable methods
java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.validateInstanc ...
- 【BZOJ-4591】超能粒子炮·改 数论 + 组合数 + Lucas定理
4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 95 Solved: 33[Submit][Statu ...
- ASCII码对照表
最近做东西常用到,所以就这里和收藏一下,需要注意的是10和13,在windows中文本java会把回车解释成是1310两个字节,在linux下面是10一个字节. 下面是将两个文件读成了二进制之后的结果 ...
- 【bzoj1597】 土地购买
http://www.lydsy.com/JudgeOnline/problem.php?id=1597 (题目链接) 题意 购买n个矩形,每块土地的价格是它的面积,但可以同时购买多快土地. 这些土地 ...