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> < ...
随机推荐
- 虚拟机VirtualBox 5.1.0|VBOX
Oracle VM VirtualBox是一款免费.开源的虚拟机软件,现属于Oracle旗下产品.可以安装Windows.Linux.IBM OS/2.Solaris.BSD等操作系统,具有远端桌面协 ...
- canvas模拟重力效果
总结 速度和加速度是动画的基础元素,其中两者都是向量,包括了一个重要因素:方向. 要学会应用 分解 和 合成 ,将速度或加速度分解到x.y轴上,然后将每条轴上的加速度或速度相加,然后再分别与物体的位置 ...
- 自定义CoordinatorLayout Behavior 隐藏Footer View
在用新的控件中,我们可以用Toolbar与CoordinatorLayout实现 向上滚动隐藏的效果,可是官方并没有找到向上隐藏底部导航的功能,有一些第三方的框架实现了. 在Android M,Coo ...
- java-commons-HttpClient超时设置setConnectionTimeout和setSoTimeout
问题 之前使用httpclient请求数据 源码方法: public static String doHttp(HttpMethod result, int timeout, String chars ...
- d3-画雷达图-圆形弧线
本文转载 终极效果 源码 index.html <!DOCTYPE html> <html> <head> <meta http-equiv="Co ...
- Android消息机制之ThreadLocal的工作原理
来源: http://blog.csdn.net/singwhatiwanna/article/details/48350919 很多人认为Handler的作用是更新UI,这说的的确没错,但是更新UI ...
- bzoj 1257
商最多有sqrt(n)个. #include<iostream> #include<cstdio> #include<cstring> #include<al ...
- bzoj 3223 splay模板题3
水题...貌似理解splay怎么维护数列了... 每个点维护一个size,它的位置就是它的size,区间翻转的话可以打标记,find的时候push_down,交换左右子树. #include<i ...
- [bzoj4551][Tjoi2016][Heoi2016]树
Description 在2016年,佳媛姐姐刚刚学习了树,非常开心. 现在她想解决这样一个问题:给定一颗有根树(根为1),有以下两种操作: 1. 标记操作:对某个结点打上标记(在最开始,只有结点1有 ...
- mvc remote的验证
1,问题 在mvc验证的时候怎么自定义验证action?比如说验证用户名是否重复. 2.解决方法 通过remote 的特性 第一参数是对应的action 第二个对应的是controller contr ...