(转载)DataTable与List<T>相互转换
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace Common
{
public class ModelHelper
{
public static List<T> TableToEntity<T>(DataTable dt) where T : new()
{
List<T> lists = new List<T>();
if (dt.Rows.Count > )
{
foreach (DataRow row in dt.Rows)
{
lists.Add(SetVal(new T(), row));
}
}
return lists;
} public static T SetVal<T>(T entity, DataRow row) where T : new()
{
Type type = typeof(T);
PropertyInfo[] pi = type.GetProperties();
foreach (PropertyInfo item in pi)
{
if (row[item.Name] != null && row[item.Name] != DBNull.Value)
{
if (item.PropertyType.IsGenericType && item.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
Type conversionType = item.PropertyType;
NullableConverter nullableConverter = new NullableConverter(conversionType);
conversionType = nullableConverter.UnderlyingType;
item.SetValue(entity, Convert.ChangeType(row[item.Name], conversionType), null);
}
else
{
item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
}
}
}
return entity;
} public static DataTable EntityToDataTable<T>(List<T> list) where T : new()
{
if (list == null || list.Count == )
{
return null;
} DataTable dataTable = new DataTable(typeof(T).Name);
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
{
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
Type conversionType = propertyInfo.PropertyType;
NullableConverter nullableConverter = new NullableConverter(conversionType);
conversionType = nullableConverter.UnderlyingType;
dataTable.Columns.Add(new DataColumn(propertyInfo.Name, conversionType));
}
else
{
dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
}
} foreach (T model in list)
{
DataRow dataRow = dataTable.NewRow();
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
{
object value = propertyInfo.GetValue(model, null);
if (value != null)
{
dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
}
else
{
dataRow[propertyInfo.Name] = DBNull.Value;
}
}
dataTable.Rows.Add(dataRow);
}
return dataTable;
}
}
}
(转载)DataTable与List<T>相互转换的更多相关文章
- 路由其实也可以很简单-------Asp.net WebAPI学习笔记(一) ASP.NET WebApi技术从入门到实战演练 C#面向服务WebService从入门到精通 DataTable与List<T>相互转换
路由其实也可以很简单-------Asp.net WebAPI学习笔记(一) MVC也好,WebAPI也好,据我所知,有部分人是因为复杂的路由,而不想去学的.曾经见过一位程序猿,在他MVC程序中, ...
- C# DataTable 和List之间相互转换的方法(转载)
来源:https://www.cnblogs.com/shiyh/p/7478241.html 一.List<T>/IEnumerable转换到DataTable/DataView 方法一 ...
- C# DataTable 和List之间相互转换的方法
介绍:List/IEnumerable转换到DataTable/DataView,以及DataTable转换到List 正文: 一.List<T>/IEnumerable转换到DataTa ...
- 转 C# DataTable 和List之间相互转换的方法
一.List/IEnumerable转换到DataTable/DataView 方法一: /// <summary> /// Convert a List{T} to a DataTabl ...
- (转载)DataTable使用技巧总结
在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.Da ...
- DataTable与List<T>相互转换
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C# datatable to list
C# DataTable 和List之间相互转换的方法 好库文章 » 软件开发 » .NET » C# 发布者:好饱 发布日期:2013-1-27 22:17:49 更新日期:2013-1-27 ...
- Linq 操作基础
参考资料: LINQ系列:LINQ to DataSet的DataTable操作 List<T>转换为DataTable C# DataTable 和List之间相互转换的方法 Linq中 ...
- 【转载】ArcEngine ITable 与System.DataTable相互转换
/// <summary> /// 打开dbf表 /// </summary> /// <param name="pathName"></ ...
随机推荐
- SQL语句练习手册--第一篇
表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师 ...
- 聚类分析算法及SAS实现
聚类分析是用户细分里面最为重要的工具,而用户细分则是整个精准营销里面的基础. 聚类分析方法分为: 层次法:可分为凝聚式和分列式,适用于观测数比较少的情形 1.凝聚式:将每个观测都归为一类,然后每次都将 ...
- python测试网页是否能正常登陆
#!/usr/bin/python #encoding:utf-8 ##实现网页的登陆检查 import HTMLParser import urlparse import cookielib imp ...
- spark sql的简单操作
测试数据 sparkStu.text zhangxs chenxy wangYr teacher wangx teacher sparksql { ,"job":"che ...
- 解决myeclipse10.x的Servers产生的at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>(Unknown Source)错
错误: java.lang.NullPointerException at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>(U ...
- FILE 创建
public class CreateDelFileUtils implements Serializable{ /** * */ private static final long serialVe ...
- instagram架构分析_转
转自:http://www.eit.name/blog/read.php?504 Instagram 团队上个月才迎来第 7 名员工,是的,7个人的团队.作为 iPhone 上最火爆的图片类工具,in ...
- Linux tar包安装Nginx
1.首先安装依赖包(依赖包有点多.我们採用yum的方式来安装) yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel ...
- 2016 acm香港网络赛 A题. A+B Problem (FFT)
原题地址:https://open.kattis.com/problems/aplusb FFT代码参考kuangbin的博客:http://www.cnblogs.com/kuangbin/arch ...
- 转载 【iOS开发】网页JS与OC交互(JavaScriptCore) OC ----->JS
目标 本文介绍利用苹果在iOS7时发布的JavaScriptCore.framework框架进行js与OC的交互.我们想要达到的目标是: OC调用网页上的js方法 网页js调用APP中的OC方法 ...