zoukankan      html  css  js  c++  java
  • 【转】将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

  • 相关阅读:
    Jetson TX1使用usb camera采集图像 (2)
    Jetson TX1安装pyTorch
    Jetson TX1 install Opencv3
    Jetson TX1使用usb camera采集图像 (1)
    win10双系统安装卸载ubuntu
    弱监督下的目标检测算法
    javascript高级程序设计读书笔记
    好用的linux命令
    正则表达式学习
    yii执行原理
  • 原文地址:https://www.cnblogs.com/q975261413/p/4011869.html
Copyright © 2011-2022 走看看