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>相互转换的更多相关文章

  1. 路由其实也可以很简单-------Asp.net WebAPI学习笔记(一) ASP.NET WebApi技术从入门到实战演练 C#面向服务WebService从入门到精通 DataTable与List<T>相互转换

    路由其实也可以很简单-------Asp.net WebAPI学习笔记(一)   MVC也好,WebAPI也好,据我所知,有部分人是因为复杂的路由,而不想去学的.曾经见过一位程序猿,在他MVC程序中, ...

  2. C# DataTable 和List之间相互转换的方法(转载)

    来源:https://www.cnblogs.com/shiyh/p/7478241.html 一.List<T>/IEnumerable转换到DataTable/DataView 方法一 ...

  3. C# DataTable 和List之间相互转换的方法

    介绍:List/IEnumerable转换到DataTable/DataView,以及DataTable转换到List 正文: 一.List<T>/IEnumerable转换到DataTa ...

  4. 转 C# DataTable 和List之间相互转换的方法

    一.List/IEnumerable转换到DataTable/DataView 方法一: /// <summary> /// Convert a List{T} to a DataTabl ...

  5. (转载)DataTable使用技巧总结

    在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结.         一.Da ...

  6. DataTable与List<T>相互转换

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. C# datatable to list

    C# DataTable 和List之间相互转换的方法 好库文章 » 软件开发 » .NET » C# 发布者:好饱  发布日期:2013-1-27 22:17:49   更新日期:2013-1-27 ...

  8. Linq 操作基础

    参考资料: LINQ系列:LINQ to DataSet的DataTable操作 List<T>转换为DataTable C# DataTable 和List之间相互转换的方法 Linq中 ...

  9. 【转载】ArcEngine ITable 与System.DataTable相互转换

    /// <summary> /// 打开dbf表 /// </summary> /// <param name="pathName"></ ...

随机推荐

  1. JavaScript 转换小技巧

    1.变量转换 看起来很简单,但据我所看到的,使用构造函数,像Array()或者Number()来进行变量转换是常用的做法.始终使用原始数据类型(有时也称为字面量)来转换变量,这种没有任何额外的影响的做 ...

  2. AspectJ风格的Aop切点表达式

    execution(*com.aptech.jb.epet.dao.hibimpl.*.*(..)) 这样写应该就可以了,这是com.aptech.jb.epet.dao.hibimpl 包下所有的类 ...

  3. ListView知识点汇总(9.2)

    1 最为基础的listview: http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html http://blog.csdn.net/h ...

  4. Intent获取Activity返回值

    /* Intent获取Activity返回值* 三步:* 子Activity关闭后的返回值处理函数,requestCode是子Activity返回的请求码,与页面顶端的两个请求码相匹配,resultC ...

  5. Java 多线程 并发编程 (转)

    一.多线程 1.操作系统有两个容易混淆的概念,进程和线程. 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进程的地址空间是互相隔离的:进程拥有各种 ...

  6. Debug与Trace工具类的应用

    在写Console程序的时候,能够使用Console.WriteLine()来时时的输出程序的执行状态和各种參数此刻的信息.可是假设是Windows Form程序,我们要怎样实时的观測程序的执行状况呢 ...

  7. Android sdk 更新失败解决方发整理

    解决办法: 设置本地hosts windows里hosts位置在C:\Windows\System32\drivers\etc,找到hosts文件 直接在hosts文件的最后加一行: 74.125.2 ...

  8. 求重集的r-组合

    具体的就不在这里说了,如果有兴趣的可以把我的工程包下载下来看,留个URL http://pan.baidu.com/s/1bnes1HX

  9. ARM初学引导_转

    一直都在听说ARM有多么好,有多神奇,有多难学.故学它时都兴奋加恐惧.呵呵,我刚好用ARM也有一段时间了.写点东西给ARM的初学者,希望能起到帮助作用. 1,记住:ARM很简单,就如从51转换到PIC ...

  10. nginx的proxy_pass到$host的问题

    今天在配置一个location的时候,希望使用一个变量如$host来指示nginx代理: location /test/ { proxy_pass http://$host; } 如你想不到,这个配置 ...