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. vue vue-router beforeRouteEnter

    beforeRouteEnter (to, from, next) { // 在渲染该组件的对应路由被 confirm 前调用 // 不!能!获取组件实例 `this` // 因为当守卫执行前,组件实 ...

  2. git个人使用总结(界面版)

    最近开始使用GIT来管理测试文档,从0到1开始使用git 1.首先,使用网页登录GIT后,创建项目 2.创建项目后,需要配置一下访问者权限 3.然后在网页版GIT复制地址,git clone到 本地 ...

  3. VBA 按列查找小工具类似lookUp函数

    如上图,查找A列的数据在D,F列是否存在,如果存在背景色变绿,如果不存在则A列的背景色变红. 直接贴上代码: Private Sub CommandButton1_Click() Call lookU ...

  4. ssh隧道远程连接mysql

    有时候比如一些云主机,没有开放3306端口,这里可以用ssh隧道来连接数据库,更加安全 注意点: 1.本地ssh可以登录远程服务器,(密码或者秘钥) 2.远程数据库需要赋权限给隧道端的ip 命令: s ...

  5. file_put_contents执行返回false,file_put_contents false(linux服务器httpd)

    file_put_contents执行返回false,file_put_contents false(linux服务器httpd) 默认下selinux是开启的查看SELinux状态:1./usr/s ...

  6. oracle中直方图的使用

    本文从不绑定变量和绑定变量两种情况讨论直方图的作用 一.不绑定变量 SQL> create table test(name varchar2(10));表已创建.SQL> insert i ...

  7. ORACLE 表空间使用率查询

    SELECT A.TABLESPACE_NAME, A.BYTES / (1024 * 1024 * 1024 ) TOTAL, B.BYTES / (1024 * 1024 * 1024 ) USE ...

  8. 机器学习14—SVD学习笔记

    test14.py #-*- coding:utf-8 import sys sys.path.append("svdRec.py") import svdRec from num ...

  9. linux实用命令备忘

    1. 卸载旧内核 sudo apt-get purge linux-image-xxx-xx-generic 2. 快速换ubuntu的源: sudo sed -i 's/vivid/wily/' / ...

  10. Robot Motion - poj 1573

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11269   Accepted: 5486 Description A ...