• 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}
    ";//得到 属性 和 属性的类型
                });
                //throw new Exception(str);//查看该类 的数据类型                
                entities.Add(entity);
            }
            return entities;
        }
    }
  • 相关阅读:
    Mysql中使用FIND_IN_SET解决IN条件为字符串时只有第一个数据可用的问题
    Mysql中游标的使用
    xcode5下cocos2dx横竖屏设置
    VUE 小点 1
    绝对定位居中
    清楚float的方法4种
    socket模拟简单的服务器
    Django + Uwsgi + Nginx 的生产环境部署
    常见排序算法
    mro之C3算法
  • 原文地址:https://www.cnblogs.com/guxingy/p/9598811.html
Copyright © 2020-2023  润新知