DataTable与List的相互转换
List转DataTable:
public static DataTable ToDataTable<T>(IEnumerable<T> collection)
{
var props = typeof(T).GetProperties();
var dt = new DataTable();
foreach (PropertyInfo pi in props)
{
// 当字段类型是Nullable<>时
Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[];
}
dt.Columns.Add(new DataColumn(pi.Name, colType));
}
if (collection.Count() > )
{
for (int i = ; i < collection.Count(); i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in props)
{
object obj = pi.GetValue(collection.ElementAt(i), null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
dt.LoadDataRow(array, true);
}
}
return dt;
}
DataTable 转List:
public class ModelConvertHelper<T> where T : new()
{
public static List<T> ConvertToModel(DataTable dt)
{
// 定义集合
List<T> ts = new List<T>(); // 获得此模型的类型
Type type = typeof(T);
string tempName = ""; foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name; // 检查DataTable是否包含此列 if (dt.Columns.Contains(tempName))
{
// 判断此属性是否有Setter
if (!pi.CanWrite) continue; object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}
DataTable与List的相互转换的更多相关文章
- DataTable和Json的相互转换
1 #region DataTable 转换为Json字符串实例方法 2 /// <summary> 3 /// GetClassTypeJosn 的摘要说明 4 /// </sum ...
- C# DataTable与实体的相互转换
using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespac ...
- datatable和list的转换
在开发中,把查询结果以DataTable返回很方便,但是在检索数据时又很麻烦,没有list<T>检索方便.但是数据以ILIST形式返回,就为我们在.NET中使用传统的数据绑定造成了不便.下 ...
- Newtonsoft.Json 与 DataTable的相互转换
1.这里下载:http://www.newtonsoft.com/products/json/ 安装: 解压下载文件,得到Newtonsoft.Json.dll 在项目中添加引用 2.引入 ...
- DataTable数据与Excel表格的相互转换
using Excel = Microsoft.Office.Interop.Excel; private static Excel.Application m_xlApp = null; /// & ...
- List<T> 和DataTable的相互转换
我用的将集合类转换为DataTable 的方法 /// <summary> /// 将集合类转换成DataTable /// </summary> /// <param ...
- C#中DataTable与XML格式的相互转换
1.DataTable转换成XML public string ConvertDataTableToXML(DataTable xmlDS) { MemoryStream stream = null; ...
- List,泛型和Datatable 的相互转换
public static DataTable ToDataTableTow(IList list) { DataTable result = new DataTable(); ) { Propert ...
- DataTable / DataSet 与 xml 的相互转换
之前做DataTable和DataSet转xml一直使用XmlSerializer 序列化完成.今天发现新方法,哇咔咔方便了很多.还不用担心Name为空时报错 static void Main(str ...
随机推荐
- 关于Could not load driverClass ${jdbc.driverClassName}问题解决方案
在spring与mybatis3整合时一直遇到Could not load driverClass ${jdbc.driverClassName}报错如果将 ${jdbc.driverClassNam ...
- Java编程基础篇第六章
构造方法 一:概念: 给对象的数据(属性)进行初始化 二:特点: a.方法名与类同名(字母大小写也要一样) b.没有返回值类型 c.没有具体的返回值 return 三:构造方法重载: 方法名相同,与返 ...
- 常用邮箱的 IMAP/POP3/SMTP 设置
通过网上查找的资料和自己的总结完成了下面的文章,看完之后相信大家对这三种协议会有更深入的理解.如有错误的地方望指正. POP3 POP3是Post Office Protocol 3的简称,即邮局协议 ...
- webstorm 介绍
最新版2017 破解 注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog.com/key. ...
- mybatis BindingException: Invalid bound statement (not found)
错误截图 解决措施 此异常的原因是由于mapper接口编译后在同一个目录下没有找到mapper映射文件而出现的. 通常我们在配置SqlSessionFactory时会有如配置 <!-- 配置Sq ...
- Vuex入门(5)—— 为什么要用Action管理异步操作
Action 类似于 mutation,不同在于: 1.Action 提交的是 mutation,而不是直接变更状态. 2.Action 可以包含任意异步操作. 官方给的定义我没什么意见,事实上我通过 ...
- python基础(14)-反射&类的内置函数
反射 几个反射相关的函数可参考python基础(10)-匿名函数&内置函数中2.2.4反射相关 类的一些内置函数 __str__()&__repr__() 重写__str__()函数类 ...
- 2019.04.13 python基础
第一节 主要讲python背景 没什么要注意的 了解记住概念就好 python官网 python.org 自带shell 可以运行python代码 在IDLE中怎么运行代码 新建文本 ...
- python数据结构-如何在列表、字典、集合中根据条件筛选数据
如何在列表.字典.集合中根据条件筛选数据 问题举例: 过滤列表[1, 2, 5, -1, 9, 10]中的负数 筛选字典{“zhangsan”:97, "lisi":80, &qu ...
- IIS相关
使用aspnet_regiis.exe重新注册.NET Framework 重新安装IIS以后,需要用aspnet_regiis.exe来注册.NET Framework, 如下: C:\WINDOW ...