C# DataTable To Entities
原地址忘了,修改过了一下。
new:
public static class DataTableEntityInteroperate
{
public static List<T> ToEntities<T>(this DataTable dt) where T : class, new()
{
if (null == dt || dt.Rows.Count == ) { return null; }
List<T> entities = new List<T>(); foreach (DataRow row in dt.Rows)
{
PropertyInfo[] pArray = typeof(T).GetProperties();
T entity = new T(); Array.ForEach<PropertyInfo>(pArray, p =>
{
object cellvalue = row[p.Name];
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;
}
}
old:
public static class DataTableEntityInteroperate
{
public static List<T> ToEntities<T>(this DataTable dt) where T : class, new()
{
if (null == dt || dt.Rows.Count == ) { return null; }
List<T> entities = new List<T>(); foreach (DataRow row in dt.Rows)
{
PropertyInfo[] pArray = typeof(T).GetProperties();
T entity = new T(); //string str = "";
Array.ForEach<PropertyInfo>(pArray, p =>
{
object value = row[p.Name];
if (value != DBNull.Value)
{
//一般用这种,这里我有点特殊
//p.SetValue(entity, value, null); 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);
}
//其它类型 暂时不管
}
//str += $"{p.Name} => {p.PropertyType.Name}\r\n";//得到 属性 和 属性的类型
});
//throw new Exception(str);//查看该类 的数据类型
entities.Add(entity);
}
return entities;
}
}
C# DataTable To Entities的更多相关文章
- 反射小应用之DataTable和List<T>互操作
在程序中,往往会遇到一些小情况,就是数据库取出来的时候为了方便直接将数据通过存储在DataSet或DataTable中,这样做的一个后果是在日后的的对数据进行”细“操作时,就发现它可能没有List&l ...
- Npgsql使用入门(三)【批量导入数据】
Program.cs代码: class Program { static void Main(string[] args) { var test = new PgBulkCopyHelper<S ...
- 获取DataTable选择第一行某一列值
数据源是一个DataTable,现在我们需要获取这个DataTable的第一行第一列的值.先准备一个数据集,创建一个DataTable,并填充数据: source code: using System ...
- LINQ查询返回DataTable类型
个人感觉Linq实用灵活性很大,参考一篇大牛的文章LINQ查询返回DataTable类型 http://xuzhihong1987.blog.163.com/blog/static/267315872 ...
- [工具类]泛型集合转换为DataTable
写在前面 在实际项目中,用到了将集合转换为DataTable,就试着封装了一个方法,记录一下. 代码 using System; using System.Collections.Generic; u ...
- 泛型集合转换为DataTable
在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...
- C# DataTable转实体 通用方法【转】
public static T GetEntity<T>(DataTable table) where T : new() { T entity = new T(); ...
- C# DataTable转实体通用方法
public static class DataTableHelper { public static T GetEntity<T>(DataTable table) where T : ...
- C#基础知识之泛型集合转换为DataTable
在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...
随机推荐
- es6(10)--Set,Map(1)
//Set { let list=new Set(); list.add(5);//添加 list.add(7); //属性size就是长度 console.log('size',list.size) ...
- 什么时候删除指针后,要给指针赋NULL
删除后需要赋NULL: 1.当在一个类里的时候,删除类的某个成员对象,需要给它赋NULL,以防其他地方使用这个成员的时候,不知道这个成员是否存在 eg: ref1::ref1() { tPint = ...
- EXCEL workbook.saveas 函数详解
本问所有资料来自于 Excel2003 VBA帮助文件,张荣整理,适用于DELPHI,VB的高级语言操作Excel用 ExcelApplication.WorkBook.SaveAs(filename ...
- scp(secure copy)安全拷贝
scp(secure copy)安全拷贝 (1)scp定义: scp可以实现服务器与服务器之间的数据拷贝.(from server1 to server2) (2)基本语法 命令 递归 要拷贝的文 ...
- 【sql小坑】在group by里用select字段的别名?
背景 -- 求每个用户的拥有的产品数,其中userid需要简单split出来 SELECT split (id, '-') [ 0 ] AS userid, count(DISTINCT produc ...
- 18.Mysql搜索引擎及其区别
这是面试中的问题:当时也是没有直接回答出来,还是因为基础知识不扎实. 一般Mysql常用的搜索引擎有:ISAM.MylSAM.HEAP.InnoDB.Berkley(BDB) ISAM:执行读取操作的 ...
- 《算法》第四章部分程序 part 17
▶ 书中第四章部分程序,包括在加上自己补充的代码,无环图最短 / 最长路径通用程序,关键路径方法(critical path method)解决任务调度问题 ● 无环图最短 / 最长路径通用程序 pa ...
- Weblogic环境(JSP)文件下载问题(下载的文件与原文件大小不一致问题)
最近发现一个问题有个download.jsp文件下载jsp在Tomcat下正常,在Weblogic下不太正常! Weblogic下载的文件比原文件大两个字节,查看文件像是文件内容最后多了空行 检查do ...
- hadoop distcp 命令& 不同hadoop 版本cp
# 1 版本相同 hadoop distcp -m 10 -bandwidth 150 hdfs://ns1/user/hive/warehouse/public.db/public_oi_fact ...
- secureCRT 设置字体时,显示字体较少问题
控制面板->字体->选择字体,右击"显示".就可以再crt中看到了.