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> < ...
随机推荐
- setTimeout和setinterval的区别
setTimeout("alert('久等了')",2000)是等待多长时间开始执行函数 setinterval(fn,1000)是每隔多长时间执行一次函数 setTimeout和 ...
- 【转】java.util.ResourceBundle使用详解
原文链接:http://lavasoft.blog.51cto.com/62575/184605/ 人家写的太好了,条理清晰,表达准确. 一.认识国际化资源文件 这个类提供软件国际化的捷径.通 ...
- Java反射机制学习与研究
Java反射机制:可以获取正在运行时的Java对象. 1.判断运行时对象对象所属的类. 2.判断运行时对象所具有的成员变量和方法. 3.还可以调用到private方法,改变private变量的值. S ...
- jquery使用淘宝接口跨域查询手机号码归属地实例
<h1>手机号码归属地查询</h1> <div class="outer"> <p>请输入手机号码</p& ...
- inline-block 左边固定,右边自适应
<body> <div class="col-md-4 left"> <div class="logo">默沙东盲讲< ...
- linux-查看系统是32位还是64位
可以用命令“getconf LONG_BIT”查看, 如果返回的结果是32则说明是32位,返回的结果是64则说明是64位. 此外还可以使用命令“uname -a”查看, 输出的结果中,如果有x86_6 ...
- 网页提示[Not allowed to load local resource: file://XXXX]错误
网页通过http 访问时, 点击打开文件的link.在Chrome 中会报 Not allowed to load local resource: file// XXXX 的错误 <!--Add ...
- 使用fiddler查看https请求
首先点击菜单栏Tools>>>Fiddler Options>>>HTTPS 把Decrypt HTTPS Traffic 复选框勾选上 勾上之后,会弹窗提示你. ...
- Android Studio上面最好用的插件
转载:http://www.jianshu.com/p/d76b60a3883d 在开发过程中,本人用的最爽的就是代码生成的插件,帮助我们自动完成大量重复简单的工作.个人也觉得代码自动生成工具是最值得 ...
- 【BZOJ-4548&3658】小奇的糖果&Jabberwocky 双向链表 + 树状数组
4548: 小奇的糖果 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 103 Solved: 47[Submit][Status][Discuss] ...