• 【转】将datatable数据转化成list


    #region 将datatable数据转化成list   public static List<T> ToList<T>(this DataTable dt) where T : class,new()
            /// <summary>
            /// 将datatable数据转化成list
            /// </summary>
            /// <typeparam name="T">泛型T</typeparam>
            /// <param name="dt">对应的datatable数据表</param>
            /// <returns>返回结果的数据集</returns>
            public static List<T> ToList<T>(this DataTable dt) where T : class,new()
            {
                Type t = typeof(T);
                PropertyInfo[] propertys = t.GetProperties();
                List<T> lst = new List<T>();
                string typeName = string.Empty;


                foreach (DataRow dr in dt.Rows)
                {
                    T entity = new T();
                    foreach (PropertyInfo pi in propertys)
                    {
                        typeName = pi.Name;
                        if (dt.Columns.Contains(typeName))
                        {
                            if (!pi.CanWrite) continue;
                            object value = dr[typeName];
                            if (value == DBNull.Value) continue;
                            if (pi.PropertyType == typeof(string))
                            {
                                pi.SetValue(entity, value.ToString(), null);
                            }
                            else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
                            {
                                pi.SetValue(entity, int.Parse(value.ToString()), null);
                            }
                            else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
                            {
                                pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
                            }
                            else if (pi.PropertyType == typeof(float))
                            {
                                pi.SetValue(entity, float.Parse(value.ToString()), null);
                            }
                            else if (pi.PropertyType == typeof(double))
                            {
                                pi.SetValue(entity, double.Parse(value.ToString()), null);
                            }
                            else
                            {
                                pi.SetValue(entity, value, null);
                            }
                        }
                    }
                    lst.Add(entity);
                }
                return lst;


                //调用
                //List<People> p = dt.ToList<People>();
            } 
            #endregion

  • 相关阅读:
    win10下无法安装loadrunner,提示“管理员已阻止你运行此应用”
    【原创】selenium+python+openpyxl实现登录自动化测试,自动读取excel用例数据,并将数据结果自动写入到excel
    用python+openpyxl从表格中读取测试用例的多条数据,然后将执行结果写入表格中
    用python+openpyxl从表格中读取测试用例的多条数据,然后将执行结果写入表格中,同时生成测试报告
    用Python添加写入数据到已经存在的Excel的xlsx文件
    selenium与webdriver驱动与firefox、 chrome匹配版本
    python3学习之lambda+sort
    小白月赛22 J : 计算 A + B
    小白月赛22 F: 累乘数字
    小白月赛22 E : 方格涂色
  • 原文地址:https://www.cnblogs.com/q975261413/p/4011869.html
Copyright © 2020-2023  润新知