惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
宝玉的分享
宝玉的分享
量子位
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
J
Java Code Geeks
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
S
SegmentFault 最新的问题
B
Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
小众软件
小众软件
The Cloudflare Blog
Y
Y Combinator Blog
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
IT之家
IT之家

博客园 - itjeff

sql server中展示数据库所有表结构的查询语句,然后可以做Excel导出 jQuery的.each()方法中return的坑 the server responded with a status of 413 (Request Entity Too Large) 解决 js 常用函数 push()、pop()、shift()、unshift()、slice()、splice() 等 Win10 HEU KMS Activator被识别为病毒怎么办? 解决equal to 运算中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_CS_AS" 之间的排序规则冲突 sql server 数据库设置不区分大小写的设置 bootstraptable 列隐藏 sql server 2014 新建授权某个表的用户 C#的小数位保留以及四舍五入 Windows 服务器远程桌面授权120天到期 sql server 清空日志操作步骤 查看SQL Server数据库各表数据量与占用空间 SQL Server 日志文件收缩 一文说清楚http、tcp、socket、websocket区别 JS中的 解构、可扩展运算符(...) Access-Control-Allow- 设置 跨域资源共享 CORS 详解 Bootstrap 模态框(Modal)插件的基本应用 js 中的相对路径的写法
list和datatable相互转化
itjeff · 2025-01-20 · via 博客园 - itjeff
/// <summary>
        /// list转datatable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static DataTable ListToDataTable<T>(IEnumerable<T> collection)
        {
            var props = typeof(T).GetProperties();
            var dt = new DataTable();
            dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            if (collection.Count() > 0)
            {
                for (int i = 0; i < collection.Count(); i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        object obj = pi.GetValue(collection.ElementAt(i), null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    dt.LoadDataRow(array, true);
                }
            }
            return dt;
        }
           
        /// <summary>
        /// datatable转list
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> DataTableToList<T>(DataTable dt) where T : new()
        {
            List<T> ts = new List<T>();// 定义集合
            Type type = typeof(T); // 获得此模型的类型
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;
                    if (dt.Columns.Contains(tempName))
                    {
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                        {
                            if(pi.PropertyType == typeof(decimal))
                                pi.SetValue(t, decimal.Parse(value.ToString()), null);
                            else if (pi.PropertyType == typeof(double))
                                pi.SetValue(t, double.Parse(value.ToString()), null);
                            else if (pi.PropertyType == typeof(float))
                                pi.SetValue(t, float.Parse(value.ToString()), null);
                            else if (pi.PropertyType == typeof(int))
                                pi.SetValue(t, int.Parse(value.ToString()), null);
                            else if (pi.PropertyType == typeof(DateTime))
                                pi.SetValue(t, DateTime.Parse(value.ToString()), null);
                            else
                                pi.SetValue(t, value, null);
                        }
                    }
                }
                ts.Add(t);
            }

            return ts;
        }