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 > 0)
{
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 == 0)
{
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之间相互转换的方法
介绍: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与List<T>相互转换
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C# DataTable 和List之间相互转换的方法(转载)
来源:https://www.cnblogs.com/shiyh/p/7478241.html 一.List<T>/IEnumerable转换到DataTable/DataView 方法一 ...
- 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中 ...
- C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换
LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...
- DataTable和List相互转换的类
DataTable与List相互转换 .NET后台数据处理,从数据库中的捞出的数据格式一般是List和DataTable的格式.现在将两种格式相互转换的心得记录下来以便以后查找(直接上代码). pub ...
随机推荐
- JavaScript自定义鼠标右键菜单
下面为JavaScript代码 window.onload = function () { //好友列表 var f = 0; //判断指定id的元素在页面中是否存在 if (document.get ...
- unity shader 常用函数列表
此篇博客转自csdn的一位大牛. 中间排版出了一些问题 Intrinsic Functions (DirectX HLSL) The following table lists the intrins ...
- 在使用可变数组过程中遇到*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'问题
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFD ...
- VSCode插件开发全攻略(二)HelloWord
更多文章请戳VSCode插件开发全攻略系列目录导航. 写着前面 学习一门新的语言或者生态首先肯定是从HelloWord开始. 您可以直接克隆我放在GitHub上vscode-plugin-demo 的 ...
- 使用jenkins自部署Coding项目
下载安装jenkins 下载地址:https://jenkins.io/download/ 安装后通过主机的8080端口进行程序设置,插件安装默认的就好 Jenkins项目目录:C:\Program ...
- 全栈开发工程师微信小程序-上(中)
全栈开发工程师微信小程序-上(中) width: 750rpx; 750rpx代表与屏幕等宽,rpx的缩写responsive pixel,这个单位是可以根据屏幕大小进行自适应调整的像素单位. 小程序 ...
- Aseprite+Cocos:打包像素画图,导入到cocos里并动起来
前言:Aseprite入门教程 Aseprite入门:第一个gif动图 1.制作像素画: 按照上一次的小球跳动制作过程,先制作一个像素画动画: 若是导出gif动态图,效果如下: ...
- [部署]CentOS安装MariaDB
环境 虚拟机:VMWare10.0.1 build-1379776 操作系统:CentOS7 64位 步骤 1.添加MariaDB的yum仓库源,在/etc/yum.repos.d/ 下建立 Mari ...
- python-图像处理(映射变换)
做计算机视觉方向,除了流行的各种深度学习算法,很多时候也要会基础的图像处理方法. 记录下opencv的一些操作(图像映射变换),日后可以方便使用 先上一张效果图 图二和图三是同一种方法,只是变换矩阵不 ...
- DotNetCore深入了解之二HttpContext类
当KestrelServer启动时,会绑定相应的IP地址,同时在绑定时将加入HttpConnectionMiddleware作为终端连接的中间件. public async Task StartAsy ...