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 == 0) { 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 == 0) { 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;
    }
}

 

转载于:https://www.cnblogs.com/guxingy/p/9598811.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值