zoukankan      html  css  js  c++  java
  • DataTable 转换为List

           注意table 列的参数类型,若不为string 需要详细声明 如 typeof(Int32) 
            public static IList<T> ConvertTo<T>(DataTable dt) where T : new()
            {
                IList<T> list = new List<T>();
                if (dt == null || dt.Rows.Count <= 0)
                {
                    return null;
                }  
                Type type = typeof(T);
                PropertyInfo[] propertys = type.GetProperties();//获取T的所有属性
                try
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        T t = new T();
                        foreach (PropertyInfo pi in propertys)
                        {  
                            if (dt.Columns.Contains(pi.Name))
                            {    
                                if (!pi.CanWrite) continue;
                                object value = dr[pi.Name];
                                if (value != DBNull.Value)
                                    pi.SetValue(t, value, null);
                            }
                        }
                        list.Add(t);
                    }
                }
                catch (Exception ex)
                {
                    list = null;
                }
                return list;
            }
  • 相关阅读:
    ReentrantLock类详解
    Linux中 docker安装
    canvas画图练习教程
    Axure 9 使用笔记
    设计模式9—工厂模式【创建型】
    Docker 快速部署 MySQL
    Laravel Sanctum SPA cookie&session 验证实践
    地图地区生成随机点
    Laravel Class 'DoctrineDBALDriverPDOMySqlDriver' not found
    LightHouse 自定义参数指令 (Argument Directives)
  • 原文地址:https://www.cnblogs.com/cherious/p/11061973.html
Copyright © 2011-2022 走看看