常用方法 DataTable转换为Entitys
备注:摘自网上 有附地址
public static List<T> DataTableToEntities<T>(this DataTable dt) where T : class, new()
{
if (null == dt || dt.Rows.Count == ) { return null; }
List<T> entities = new List<T>();
List<string> columnNames = new List<string>(); for (int i = ; i < dt.Columns.Count; i++)
{
columnNames.Add(dt.Columns[i].ColumnName);
} foreach (DataRow row in dt.Rows)
{
PropertyInfo[] pArray = typeof(T).GetProperties();
T entity = new T(); Array.ForEach<PropertyInfo>(pArray, p =>
{
if (!columnNames.Contains(p.Name))
{
return;
} object cellvalue = row[p.Name]; //空值不处理
if (cellvalue == DBNull.Value)
{
return;
}
if ((cellvalue == null) || string.IsNullOrWhiteSpace(cellvalue.ToString().Trim()))
{
return;
} if (cellvalue != DBNull.Value)
{
//经过了几个版本的迭代,最后一个为最新的,摘自网上,已附原文地址 //4、原地址:https://blog.csdn.net/Simon1003/article/details/80839744
if (!p.PropertyType.IsGenericType)
{
p.SetValue(entity, Convert.ChangeType(cellvalue, p.PropertyType), null);
}
else
{
Type genericTypeDefinition = p.PropertyType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
p.SetValue(entity, Convert.ChangeType(cellvalue, Nullable.GetUnderlyingType(p.PropertyType)), null);
}
else
{
throw new Exception("genericTypeDefinition != typeof(Nullable<>)");
}
} //3、原地址:https://blog.csdn.net/hebbers/article/details/78957569
//Type type = p.PropertyType;
//if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类
//{
// //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
// System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(type);
// //将type转换为nullable对的基础基元类型
// type = nullableConverter.UnderlyingType;
//}
//p.SetValue(entity, Convert.ChangeType(cellvalue, type), null); //2、自定义 这种很傻,但当前解决速度最快
//if (p.PropertyType.Name.Equals("Int32"))
//{
// p.SetValue(entity, Convert.ToInt32(value), null);
//}
//else if (p.PropertyType.Name.Equals("String"))
//{
// p.SetValue(entity, Convert.ToString(value), null);
//}
//else if (p.PropertyType.Name.Equals("Nullable`1"))
//{
// p.SetValue(entity, Convert.ToInt32(value), null);
//}
////其它类型 暂时不管 //1、字段不为空可以用这种
//p.SetValue(entity, value, null);
}
});
entities.Add(entity);
}
return entities;
}
常用方法 DataTable转换为Entitys的更多相关文章
- 常用方法 DataTable转换为Html
点击单元格 可以输出行和列,这个功能可以在一些特殊的地方用 public static string GetHtmlString(DataTable dt) { StringBuilder sb = ...
- 再谈使用Emit把Datatable转换为对象集合(List<T>)
一.前因和存在的问题 前面我写了一篇<使用Emit把Datatable转换为对象集合(List<T>)>的博文,其实起源于我自己编写的一个orm工具(见前面几篇博文有介绍),里 ...
- 使用Emit把Datatable转换为对象集合(List<T>)
Emit生成动态方法部分摘自网上,但是经过修改,加入了对委托的缓存以及类结构的调整,使之调用更简洁方便.大致的思路是:要实现转换datatable到某个指定对象的集合,本质是实现转换一个datarow ...
- 对象列表转换为DataTable或DataTable转换为对象列表.
/**********************************************************************************/ // 说明: 数据转换工具. ...
- 使用扩展方法将DataTable转换为List<T>
在将DataTable转换为List<T>时,找到了网上的方案,原文链接:http://stackoverflow.com/questions/4593663/fetch-datarow- ...
- 把DataTable转换为泛型List<T>或是JSON
在开发ASP.NET Web API或ASP.NET MVC时,我们从数据库得到的数据往往是DataSet或是DataTable.为了能让前端JQuery能方便使用至这些数据,我们需要把这些数据转换为 ...
- DataTable转换为List<T>或者DataRow转换为T
这段时间开发ASP.NETMVC应用程序,从数据库获取数据之后,需要把记录转换为数据集在视图中显示.我们需要把DataTable转换为List<T>或者DataRow转换为T. 本篇中可以 ...
- C#中把Datatable转换为Json的5个代码实例
一. /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table" ...
- 三层架构中bll层把datatable转换为实体model的理解
看了很多人的项目,很多都是用到三层架构,其中BLL层中有一种将DataTable转换为实体的方法.一直没有明白为啥要这样做,今天特意去搜索了一下,如果没有答案我是准备提问,寻求解答了.还好找到一个相关 ...
随机推荐
- 《即时消息技术剖析与实战》学习笔记1——IM系统的架构
一.IM的应用场景 聊天.直播.在线客服.物联网等所有需要实时互动.高实时性的场景,都需要应用到 IM 技术.
- Shell学习笔记之关于 >/dev/null 2>&1 详解
shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义输出 分解这个组合:“>/dev/null 2>&1” 为五 ...
- IIS 图片 JS CSS 500错误
1.检查站点MIME类型是否可以正常加载
- 如何在.Net Mvc中让Get,Post请求访问同一个Action的方法
[HttpPost] [ActionName("Index")] public ActionResult Post(Models.WeChatRequestModel model) ...
- [大数据学习研究] 错误排查,Hadoop集群部分DataNode不能启动
错误现象 不知道什么原因,今天发现我的hadoop集群启动后datanode只有一台了,我的集群本来有三台的,怎么只剩一台了呢? 用jps命令检查一下,发现果然有两台机器的DataNode没有启动. ...
- kylin安装过程问题排查
问题:日志报错:/usr/local/apps/kylin/tomcat/conf/.keystore (没有那个文件或目录) 解决:在kylin内置tomcat的server.xml中里边有个对ht ...
- English--动词时态
English|动词时态 时态是一个很玄乎的东西,要么是完全掌握,要么是不知所云. 在正式开始之前,大家需要明白汉语的谓语动词是不会随着时间与状态而变化.但是,英语的谓语动词会随着时间与状态发生变化. ...
- Executor的线程代码
package com.open1111; import java.util.concurrent.ExecutorService;import java.util.concurrent.Execut ...
- tensorflow 镜像
https://mirrors.tuna.tsinghua.edu.cn/tensorflow/windows/cpu/ 报错 不支持 C:\Users\brady\.conda\envs\tenso ...
- Django-ModelFrom中修改save后的字段值
在ModelForm提交中,保持原未修改字段的值,views中部分代码: project = Iredmail.objects.get(id=id) ssh_crt_name = project.ss ...