原地址忘了,修改过了一下。

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

  1. 反射小应用之DataTable和List<T>互操作

    在程序中,往往会遇到一些小情况,就是数据库取出来的时候为了方便直接将数据通过存储在DataSet或DataTable中,这样做的一个后果是在日后的的对数据进行”细“操作时,就发现它可能没有List&l ...

  2. Npgsql使用入门(三)【批量导入数据】

    Program.cs代码: class Program { static void Main(string[] args) { var test = new PgBulkCopyHelper<S ...

  3. 获取DataTable选择第一行某一列值

    数据源是一个DataTable,现在我们需要获取这个DataTable的第一行第一列的值.先准备一个数据集,创建一个DataTable,并填充数据: source code: using System ...

  4. LINQ查询返回DataTable类型

    个人感觉Linq实用灵活性很大,参考一篇大牛的文章LINQ查询返回DataTable类型 http://xuzhihong1987.blog.163.com/blog/static/267315872 ...

  5. [工具类]泛型集合转换为DataTable

    写在前面 在实际项目中,用到了将集合转换为DataTable,就试着封装了一个方法,记录一下. 代码 using System; using System.Collections.Generic; u ...

  6. 泛型集合转换为DataTable

    在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...

  7. C# DataTable转实体 通用方法【转】

    public static T GetEntity<T>(DataTable table) where T : new()    {        T entity = new T();  ...

  8. C# DataTable转实体通用方法

    public static class DataTableHelper { public static T GetEntity<T>(DataTable table) where T : ...

  9. C#基础知识之泛型集合转换为DataTable

    在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...

随机推荐

  1. Spark2.X分布式弹性数据集

    跑一下这个结果 参考代码 package com.spark.test import org.apache.spark.sql.SparkSession import org.apache.spark ...

  2. Codeforces Round #492 (Div. 2)

    A. /* 从大往小依次除 */ #include<cstdio> #include<algorithm> #include<cstring> #include&l ...

  3. Oracle:Authid Current_User的使用

    我们知道,用户拥有的role权限在存储过程是不可用的.遇到这种情况,我们一般需要显式授权,如grant create table to usera;但这种方法太麻烦,有时候可能需要进行非常多的授权才能 ...

  4. springboot中JPA的应用

    1.JPA JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据.他的出现主要是为了简 ...

  5. 企业项目构建学习(一)maven

    <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...

  6. spring boot 下载

  7. 代码:css3动画效果demo

    四行文字会逐次掉落:  如果要停留在最后一帧的动画,注意要用forwards,不要用both. <style type="text/css"> @-webkit-key ...

  8. Http协议基础知识

    r equest POST https://re.csdn.net/csdnbi HTTP/1.1方法 url/uri 协议的版本号 1.1Host: re.csdn.netConnection: k ...

  9. c# word操作

    合并单元格  http://www.360doc.com/content/11/0729/21/2097544_136620405.shtml

  10. 白鹭引擎 - 对象的添加与删除 ( 开关效果 addChild, removeChild )

    class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须调用父类的构造函数 ...